Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow creating modules using specified target names #168

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Act/Create.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Scene.New qualified as New

create :: Config -> App ()
create cfg = do
newModule <- New.constructDefaultModule (moduleName cfg)
newModule <- New.constructDefaultModule (moduleName cfg) (targetName cfg)
Initialize.initializeLogger (remarkCfg cfg)
Initialize.initializeCompilerWithModule newModule
New.createNewProject (moduleName cfg) newModule
Expand Down
9 changes: 9 additions & 0 deletions src/Context/OptParse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,20 @@ parseLSPOpt = do
parseCreateOpt :: Parser Command
parseCreateOpt = do
moduleName <- argument str (mconcat [metavar "MODULE", help "The name of the module"])
targetName <-
optional $
strOption $
mconcat
[ long "target",
metavar "TARGET_NAME",
help "The name of the target"
]
remarkCfg <- remarkConfigOpt
pure $
Create $
Create.Config
{ Create.moduleName = T.pack moduleName,
Create.targetName = targetName,
Create.remarkCfg = remarkCfg
}

Expand Down
1 change: 1 addition & 0 deletions src/Entity/Config/Create.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import Entity.Config.Remark qualified as Remark

data Config = Config
{ moduleName :: T.Text,
targetName :: Maybe T.Text,
remarkCfg :: Remark.Config
}
26 changes: 13 additions & 13 deletions src/Scene/New.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Context.Remark qualified as Remark
import Context.Throw qualified as Throw
import Control.Monad
import Data.HashMap.Strict qualified as Map
import Data.Maybe (fromMaybe)
import Data.Text qualified as T
import Entity.ClangOption qualified as CL
import Entity.Const
Expand All @@ -28,15 +29,16 @@ createNewProject moduleName newModule = do
if moduleDirExists
then Throw.raiseError' $ "The directory `" <> moduleName <> "` already exists"
else do
createModuleFile
createMainFile
createModuleFile newModule
createMainFile newModule
Remark.printNote' $ "Created a module: " <> moduleName

constructDefaultModule :: T.Text -> App Module
constructDefaultModule name = do
constructDefaultModule :: T.Text -> Maybe T.Text -> App Module
constructDefaultModule moduleName mTargetName = do
let targetName = fromMaybe moduleName mTargetName
currentDir <- Path.getCurrentDir
moduleRootDir <- Path.resolveDir currentDir $ T.unpack name
mainFile <- Path.parseRelFile $ T.unpack name <> sourceFileExtension
moduleRootDir <- Path.resolveDir currentDir $ T.unpack moduleName
mainFile <- Path.parseRelFile $ T.unpack targetName <> sourceFileExtension
return $
Module
{ moduleID = MID.Main,
Expand All @@ -45,7 +47,7 @@ constructDefaultModule name = do
moduleSourceDir = sourceRelDir,
moduleTarget =
Map.fromList
[ ( name,
[ ( targetName,
TargetSummary
{ entryPoint = SL.SourceLocator mainFile,
clangOption = CL.empty
Expand All @@ -64,17 +66,15 @@ constructDefaultModule name = do
modulePresetMap = Map.empty
}

createModuleFile :: App ()
createModuleFile = do
newModule <- Module.getMainModule
createModuleFile :: Module -> App ()
createModuleFile newModule = do
Path.ensureDir $ parent $ moduleLocation newModule
Module.saveEns (moduleLocation newModule) ([], (toDefaultEns newModule, []))
buildDir <- Path.getBaseBuildDir newModule
Path.ensureDir buildDir

createMainFile :: App ()
createMainFile = do
newModule <- Module.getMainModule
createMainFile :: Module -> App ()
createMainFile newModule = do
Path.ensureDir $ getSourceDir newModule
forM_ (getTargetPathList newModule) $ \mainFilePath -> do
Path.writeText mainFilePath "define main(): unit {\n print(\"Hello, world!\\n\")\n}\n"
Loading