LoginSignup
15
14

More than 5 years have passed since last update.

stackのオレオレテンプレートを作って楽をする

Last updated at Posted at 2016-04-17

TL;DR

  1. .hsfilesファイルを作る
  2. githubに上げる
  3. rawのurlを指定してstack new

ローカルの.hsfilesファイルを指定してもok

毎回ダルい

stack、素晴らしいですよね。cabalで依存地獄で死にながら開発していた頃とは大違いです。

しかしプログラマの性なのか、いくら便利になってもどこかに無駄があれば省きたくなるものです。私の場合はテンプレート周りで無駄をしていると思ったのでちょこっと調べて、この記事を書くに至りました。

私はstack newでnew-templateをよく使っていたのですが、このテンプレートはsrc/Lib.hsに勝手にsomeFuncという関数が作られていたり(しかもご丁寧にそれだけエクスポートするように書かれてる)、app/Main.hsにはそのsomeFuncを使うようなコードが書かれています。これを毎回消すのは非常にダルいです。

更に欲を言えば、いつも使うようなパッケージ(transformersとかcontainersとか)を毎回.cabalに書くのもダルいです。

というわけでオレオレテンプレートを作って楽しようぜ!

.hsfilesを作る

stack-templatesに置いてあるファイルを参考にぼくのかんがえたさいきょうのてんぷれーとを書きましょう。

一応、私が書いたやつを載せておきます。new-templateを少しいじっただけなので特に面白いものではないですが。

my-template.hsfiles
{-# START_FILE {{name}}.cabal #-}
name:                {{name}}
version:             0.1.0.0
synopsis:            Initial project template from stack
description:         Please see README.md
homepage:            https://github.com/{{github-username}}{{^github-username}}githubuser{{/github-username}}/{{name}}#readme
license:             BSD3
license-file:        LICENSE
author:              {{author-name}}{{^author-name}}Author name here{{/author-name}}
maintainer:          {{author-email}}{{^author-email}}example@example.com{{/author-email}}
copyright:           {{copyright}}{{^copyright}}{{year}}{{^year}}2016{{/year}} {{authorName}}{{^authorName}}Author name here{{/authorName}}{{/copyright}}
category:            {{category}}{{^category}}Web{{/category}}
build-type:          Simple
-- extra-source-files:
cabal-version:       >=1.10

library
  hs-source-dirs:      src
  exposed-modules:     Lib
  build-depends:       base >= 4.7 && < 5
                     , transformers
  default-language:    Haskell2010

executable {{name}}-exe
  hs-source-dirs:      app
  main-is:             Main.hs
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  build-depends:       base
                     , {{name}}
  default-language:    Haskell2010

test-suite {{name}}-test
  type:                exitcode-stdio-1.0
  hs-source-dirs:      test
  main-is:             Spec.hs
  build-depends:       base
                     , {{name}}
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  default-language:    Haskell2010

source-repository head
  type:     git
  location: https://github.com/{{github-username}}{{^github-username}}githubuser{{/github-username}}/{{name}}

{-# START_FILE Setup.hs #-}
import Distribution.Simple
main = defaultMain

{-# START_FILE test/Spec.hs #-}
main :: IO ()
main = putStrLn "Test suite not yet implemented"

{-# START_FILE src/Lib.hs #-}
module Lib where

{-# START_FILE app/Main.hs #-}
module Main where

import Lib

main :: IO ()
main = return ()

{-# START_FILE LICENSE #-}
Copyright {{author-name}}{{^author-name}}Author name here{{/author-name}} (c) {{year}}{{^year}}2016{{/year}}

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of {{author-name}}{{^author-name}}Author name here{{/author-name}} nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

この時点でstack new.hsfilesファイルを指定することでテンプレートとして使うことができるようになります。

$ stack new my-project my-template.hsfiles

ローカルに置いておいておけば十分な場合も多そうです。

githubに上げる

Allow stack new to download templates over HTTP(S)
Changelog - The Haskell Tool Stack

上記のように、stackはバージョン1.0でstack newにurlを渡すとそれをダウンロードしてくれるようになったので利用してみます。

と言っても今回は適当なリポジトリに.hsfilesファイルを入れてgithubにpushするだけです。githubは便利、ちぃ、おぼえたえ?githubは時々落ちてる?そんなことは知らん

使う

pushした.hsfilesファイルのrawページのurlを指定してstack new

$ stack new my-project https://raw.githubusercontent.com/ほげほげ

これでどこからでもオレオレテンプレートが使えるようになりました。

ふと思ったんですけど、urlにパラメータを埋め込んで、apiを叩くと良い感じのhsfilesファイルを吐き出してくれるようなwebサービスとかあったら面白そうですね。

ほな、また……。

15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14