diff --git a/init-haskell-project.sh b/init-haskell-project.sh index a524c83..a19d4da 100755 --- a/init-haskell-project.sh +++ b/init-haskell-project.sh @@ -1,39 +1,104 @@ #!/usr/bin/env zsh +autoload colors +colors +for COLOR in RED GREE YELLOW BLUE MAGENTA CYAN BLACK WHITE; do + eval $COLOR='$fg_no_bold[${(L)COLOR}]' + eval BOLD_$COLOR='$fg_bold[${(L)COLOR}]' +done +eval RESET='$reset_color' +# --- Function declaration and global variables answer="" +# Capitalize a string +capitalize(){ + local str="$*" + print -- ${${str[1]}:u}${str[2,-1]} +} +bk(){print -- "${RED}Bridgekeeper: $*${RESET}"} +bkn(){print -n -- "${RED}Bridgekeeper: $*${RESET}"} +you(){print -- "${BLUE}You: $*${RESET}"} +log(){print -- $*} +err(){ + { + case $(( $RANDOM % 1 )); in + 0) bk "What... is your favourite colour?" + you "Blue. No, yel..." + log "[You are thrown over the edge into the volcano]" + you "You: Auuuuuuuuuuuugh" + bk " Hee hee heh." + ;; + 1) bk "What is the capital of Assyria?" + you "I don't know that!" + log "[You are thrown over the edge into the volcano]" + you "Auuuuuuuuuuuugh" + ;; + esac + + print -- "\n$*" + }>&2 + exit 1 +} +# Ask the user for some information +# Search in .gitconfig some hints to provide default value ask(){ local elem=$1 local gitconfigelem - print -n -- "What is your $elem?" + bkn "What is your $elem?" [[ -e ~/.gitconfig ]] && { gitconfigelem="$(< ~/.gitconfig| awk '$1 == "'$elem'" {$1="";$2="";gsub("^ *","");print}')" [[ $gitconfigelem = "" ]] || { print -n -- " ($gitconfigelem)" } } - print -n ": " - read answer + print -n "\n> ";read answer [[ $answer = "" ]] && answer=$gitconfigelem } -print -n -- "What is the name of your project? (start with a capital)" -read project +# Delete the project directory and show an error message +reinit(){ + cd .. + [[ -e $project ]] && \rm -rf $project + err "Something went wrong, I removed the project directory" +} -err(){print -- $*>&2;exit 1} + +# --- Start asking questions +bk "Stop!" +bk "Who would cross the Bridge of Death" +bk "must answer me these questions three," +bk "ere the other side he see." +you "Ask me the questions, bridgekeeper, I am not afraid.\n" + +# project name +bk "What is the name of your project?" +print -n "> ";read project + +if perl -e 'exit("'$project'" =~ /^[a-zA-Z0-9]*$/)'; then + err "The project name can't contains non ASCII character" +fi +project=${project:l} # use lowercase for project name +module=$(capitalize $project) [[ $project = "" ]] && err "Can't use empty project name" -[[ -e $project ]] && err "$project already exists" +[[ -e $project ]] && err "$project directory already exists" +# author name ask name name="$answer" +# email ask email email="$answer" -print -n -- "A short description of the project: " -read description -print -n -- "What is your github user name? " -read github +# github +bk "What is your github user name?" +print -n "> ";read github +# synopsis +bk "What is your project in less than ten words?" +print -n "> ";read description + +# --- We start the creation of the project files here mkdir $project cd $project +print -- "Initialize git" git init . print -- ".gitignore" @@ -46,7 +111,8 @@ dist .ghci END -print -- "$project.cabal" +print -- "Create files" +print -- "\t$project.cabal" > $project.cabal cat <=1.10 -executable yml +executable $project main-is: Main.hs -- other-modules: -- other-extensions: @@ -76,9 +142,9 @@ executable yml default-language: Haskell2010 library - exposed-modules: $project - , $project.Foo - , $project.Bar + exposed-modules: $module + , $module.Swallow + , $module.Coconut -- other-modules: -- other-extensions: build-depends: base >=4.6 && <4.7 @@ -86,7 +152,7 @@ library hs-source-dirs: src default-language: Haskell2010 -executable test-yml +executable test-$project hs-source-dirs: test ghc-options: -Wall main-is: Test.hs @@ -107,7 +173,6 @@ test-suite Tests Type: exitcode-stdio-1.0 default-language: Haskell2010 build-depends: base ==4.6.*, Cabal >= 1.16.0 - , vector , $project , HUnit , QuickCheck @@ -118,13 +183,13 @@ test-suite Tests , tasty-smallcheck END -print -- "Setup.hs" +print -- "\tSetup.hs" > Setup.hs cat < LICENSE cat< test/Test.hs cat < test/$project/Foo/Test.hs cat < test/$module/Swallow/Test.hs cat < src/$project/Foo.hs cat < src/$module/Swallow.hs cat < String -> String -foo prefix suffix = prefix ++ suffix +swallow :: String -> String -> String +swallow prefix suffix = prefix ++ suffix END -print -- "test/$project/Bar/Test.hs" -> test/$project/Bar/Test.hs cat < test/$module/Coconut/Test.hs cat < Serial m BarDataStruct where series = cons1 BarDataStruct --- Now we could test properties with smallcheck on BarDataStruct type. +instance Monad m => Serial m CoconutDataStruct where series = cons1 CoconutConstr +-- Now we could test properties with smallcheck on CoconutDataStruct type. -barSuite :: TestTree -barSuite = testGroup "bar" - [ testCase "bar" testBar - , SC.testProperty "bar property" prop_bar +coconutSuite :: TestTree +coconutSuite = testGroup "coconut" + [ testCase "coconut" testCoconut + , SC.testProperty "coconut property" prop_coconut ] -testCost :: Assertion -testCost = bar @=? 10 +testCoconut :: Assertion +testCoconut = coconut @=? 10 -prop_cost_positive :: Property IO -prop_cost_positive = forAll $ \barStruct -> barfunc barStruct >= 0 +prop_coconut :: Property IO +prop_coconut = forAll $ \coconutStruct -> coconutfunc coconutStruct >= 0 END -print -- "src/$project/Bar.hs" -> src/$project/Bar.hs cat < src/$module/Coconut.hs cat < Int -barfunc (BarConstr l) = length l +coconutfunc :: CoconutDataStruct -> Int +coconutfunc (CoconutConstr l) = length l END -cabal sandbox init +print -- "\tsrc/$module.hs" +> "src/$module.hs" cat < "src/Main.hs" cat <