From 3c4852f9024630a22e0b47df467bc7f0ac5c5508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Hahn?= Date: Thu, 2 Nov 2023 16:57:25 -0400 Subject: [PATCH] [garn init] Handle non-inferrable projects better (#338) --- test/spec/InitSpec.hs | 25 +++++++++++++++- ts/internal/init.ts | 66 ++++++++++++++++++++++++++----------------- 2 files changed, 64 insertions(+), 27 deletions(-) diff --git a/test/spec/InitSpec.hs b/test/spec/InitSpec.hs index 2fcfef20..be7b8f05 100644 --- a/test/spec/InitSpec.hs +++ b/test/spec/InitSpec.hs @@ -2,6 +2,7 @@ module InitSpec where +import Control.Monad (forM_, when) import Data.String.Interpolate (i) import Data.String.Interpolate.Util (unindent) import Development.Shake (StdoutTrim (..), cmd, cmd_) @@ -129,10 +130,32 @@ spec = do stderr output `shouldBe` unindent [i| - [garn] Creating a garn.ts file [garn] Found but could not parse cabal file + [garn] Cannot detect any project toolchains, sorry! Creating example garn.ts file |] + describe "when no specific initializer runs" $ do + it "prints out a message about that" $ \onTestFailureLog -> do + output <- runGarn ["init"] "" repoDir Nothing + onTestFailureLog output + stderr output + `shouldBe` unindent + [i| + [garn] Cannot detect any project toolchains, sorry! Creating example garn.ts file + |] + + it "generates a commented file, except for imports" $ \onTestFailureLog -> do + output <- runGarn ["init"] "" repoDir Nothing + onTestFailureLog output + garnFile <- lines <$> readFile "garn.ts" + take 2 garnFile + `shouldBe` [ "import * as garn from \"https://garn.io/ts/v0.0.14/mod.ts\";", + "import * as pkgs from \"https://garn.io/ts/v0.0.14/nixpkgs.ts\";" + ] + forM_ (drop 2 garnFile) $ \line -> + when (line /= "") $ + line `shouldStartWith` "//" + rewriteImportsToLocalhost :: IO () rewriteImportsToLocalhost = do cmd_ "sd" "https://garn.io/ts/v[0-9]+\\.[0-9]+\\.[0-9]+/" "http://localhost:8777/" "garn.ts" diff --git a/ts/internal/init.ts b/ts/internal/init.ts index 6eb4534b..fc43f348 100644 --- a/ts/internal/init.ts +++ b/ts/internal/init.ts @@ -16,8 +16,6 @@ const initializers = [ ...javascript.initializers, ]; -console.error("[garn] Creating a garn.ts file"); - for (const init of initializers) { const result = init(Deno.cwd()); switch (result.tag) { @@ -34,45 +32,61 @@ for (const init of initializers) { } if (initializedSections.length === 0) { + console.error( + "[garn] Cannot detect any project toolchains, sorry! Creating example garn.ts file", + ); initializedSections.push(outdent` // Welcome to garn! \`garn init\` was unable to find any existing supported // projects, but it is easy to get started! + // Check out the language helper functions under garn.go, garn.haskell, and // garn.javascript. // // For example: - export const myGoProject = garn.go.mkGoProject({ - description: "My go project", - src: "./my-go-project", - goVersion: "1.20", - }); - - export const myHaskellProject = garn.haskell.mkHaskellProject({ - description: "My haskell project", - executable: "server", - compiler: "ghc94", - src: "./my-haskell-project", - }); - - export const myNodeProject = garn.javascript.mkNpmProject({ - description: "My node project", - src: "./my-node-project", - nodeVersion: "18", - }); + + // export const myGoProject = garn.go.mkGoProject({ + // description: "My go project", + // src: "./my-go-project", + // goVersion: "1.20", + // }); + + // export const myHaskellProject = garn.haskell.mkHaskellProject({ + // description: "My haskell project", + // executable: "server", + // compiler: "ghc94", + // src: "./my-haskell-project", + // }); + + // export const myNodeProject = garn.javascript.mkNpmProject({ + // description: "My node project", + // src: "./my-node-project", + // nodeVersion: "18", + // }); + // You can also manually create environments and projects. For example // uncomment this block and you can run \`garn enter myProject\` to be put into a // shell with cowsay installed, and \`garn run myProject\` to execute the default // executable for this project. - const myProjectEnvironment = garn.mkEnvironment().withDevTools([pkgs.cowsay]); - export const myProject = garn.mkProject({ - description: "My project", - defaultEnvironment: myProjectEnvironment, - defaultExecutable: myProjectEnvironment.shell\`cowsay "Hello from garn!"\`, - }, {}); + // const myProjectEnvironment = garn.mkEnvironment().withDevTools([pkgs.cowsay]); + + // export const myProject = garn.mkProject({ + // description: "My project", + // defaultEnvironment: myProjectEnvironment, + // defaultExecutable: myProjectEnvironment.shell\`cowsay "Hello from garn!"\`, + // }, {}); + + + // In order to get an isolated vscodium that ships with Deno and is + // pre-configured to use Deno's LSP, you can use the following: + + // Run with \`garn run edit\`. + // export const edit = garn.editGarnConfig; `); +} else { + console.error("[garn] Creating a garn.ts file"); } Deno.writeTextFileSync(