Skip to content

Commit

Permalink
Create src/ files
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahbeckford committed Dec 7, 2024
1 parent 9b2be23 commit d434329
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
23 changes: 17 additions & 6 deletions cmake/scripts/dkcoder/project/init.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,19 @@ macro(dkcoder_project_init)
set(init_OPTIONS)
# all DkStd_Std commands must run in old versions of DkCoder. Confer dkcoder/src/DkStd_Std/README.md
set(dk_run DkRun_V2_2.RunAway)
if(NOT ARG_QUIET)
if(NOT ARG_QUIET)
string(APPEND init_OPTIONS " -verbose")
endif()

set(init_ARGS)
set(postinstall_WIN32)
set(postinstall_UNIX)
if(NOT ARG_NOSRC)
string(APPEND init_ARGS " MyScripts_Std.StartHere=")
set(postinstall_WIN32 "ECHO suggestion: Run with `./dk MyScripts_Std.StartHere`\nECHO suggestion: Open project in VSCode and install the recommended plugins.")
set(postinstall_UNIX "echo 'suggestion: Run with `./dk MyScripts_Std.StartHere`'\necho 'suggestion: Open project in VSCode and install the recommended plugins.'")
endif()

# Run the command as a post script. Why?
# 1. We don't want to run ./dk inside of ./dk. Not sure how the environment variables (etc.) interact.
# 2. Makes it possible for DkStd_Std.Project.Init to erase the dkcoder/ project directory on Windows
Expand All @@ -137,12 +146,13 @@ REM 1. We copy the dk.cmd and __dk.cmake to the new project directory
CALL "@dk_cmd_NATIVE@" @dk_run@ --generator dune --you-dir "@CMAKE_SOURCE_DIR_NATIVE@\src" -- DkStd_Std.Project.Init -windows-boot @init_OPTIONS@ "@DKCODER_PWD_NATIVE@" "@CMAKE_SOURCE_DIR_NATIVE@"
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%

REM 2. We do the dkcoder/ project deletion from the new project directory.
REM 2. We do the dkcoder/ project deletion and src/ construction from the new project directory.
REM The deletion will not erase the running dk.cmd script and cause 'The system cannot find the path specified.'
CALL "@DKCODER_PWD_NATIVE@\dk.cmd" @dk_run@ --generator dune --you-dir "@CMAKE_SOURCE_DIR_NATIVE@\src" -- DkStd_Std.Project.Init -delete-dkcoder-after @init_OPTIONS@ "@DKCODER_PWD_NATIVE@" "@CMAKE_SOURCE_DIR_NATIVE@"
CALL "@DKCODER_PWD_NATIVE@\dk.cmd" @dk_run@ --generator dune --you-dir "@CMAKE_SOURCE_DIR_NATIVE@\src" -- DkStd_Std.Project.Init -delete-dkcoder-after @init_OPTIONS@ "@DKCODER_PWD_NATIVE@" "@CMAKE_SOURCE_DIR_NATIVE@" @init_ARGS@
IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%

ECHO dkcoder: project installed.
@postinstall_WIN32@

REM The parent dk.cmd script was deleted, and if it continued then
REM we would get 'The system cannot find the path specified.' so exit
Expand All @@ -154,8 +164,9 @@ EXIT 0
file(CONFIGURE OUTPUT "${DKCODER_POST_SCRIPT}" CONTENT [[#!/bin/sh
set -euf
cd '@CMAKE_CURRENT_BINARY_DIR@'
'@dk_cmd@' @dk_run@ --generator dune --you-dir '@CMAKE_SOURCE_DIR@/src' -- DkStd_Std.Project.Init -delete-dkcoder-after @init_OPTIONS@ '@DKCODER_PWD@' '@CMAKE_SOURCE_DIR@'
ECHO "dkcoder: project installed."
'@dk_cmd@' @dk_run@ --generator dune --you-dir '@CMAKE_SOURCE_DIR@/src' -- DkStd_Std.Project.Init -delete-dkcoder-after @init_OPTIONS@ '@DKCODER_PWD@' '@CMAKE_SOURCE_DIR@' @init_ARGS@
echo "dkcoder: project installed."
@postinstall_UNIX@
]]
@ONLY NEWLINE_STYLE UNIX)
endif()
Expand All @@ -165,7 +176,7 @@ function(run)
# Get helper functions from this file
include(${CMAKE_CURRENT_FUNCTION_LIST_FILE})

set(noValues HELP MOREHELP QUIET)
set(noValues HELP MOREHELP QUIET NOSRC)
set(singleValues LOGLEVEL)
set(multiValues)
cmake_parse_arguments(PARSE_ARGV 0 ARG "${noValues}" "${singleValues}" "${multiValues}")
Expand Down
34 changes: 32 additions & 2 deletions src/DkStd_Std/Project/Init.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,38 @@ module Arg = Tr1Stdlib_V414CRuntime.Arg
module Bos = Tr1Bos_Std.Bos
module Format = Tr1Stdlib_V414CRuntime.Format
module Printf = Tr1Stdlib_V414CRuntime.Printf
module List = Tr1Stdlib_V414Base.List
module Logs = Tr1Logs_Std.Logs
module Map = Tr1Stdlib_V414Base.Map
module Out_channel = Tr1Stdlib_V414CRuntime.Out_channel
module StdExit = Tr1Stdlib_V414CRuntime.StdExit
module String = Tr1Stdlib_V414Base.String
module Stringext = Tr1String_Ext.Stringext
module Sys = Tr1Stdlib_V414CRuntime.Sys
let prerr_endline = Tr1Stdlib_V414Io.StdIo.prerr_endline
let exit = Tr1Stdlib_V414CRuntime.StdExit.exit

module StringMap = Map.Make (String)

let verbose = ref false
let windows_boot = ref false
let delete_dkcoder_after = ref false
let new_project_dir = ref ""
let dkcoder_project_dir = ref ""
let module_id_contents = ref StringMap.empty

let anon_fun s =
if !new_project_dir = "" then
new_project_dir := s
else if !dkcoder_project_dir = "" then
dkcoder_project_dir := s
else
match Stringext.cut ~on:"=" s with
| None -> ()
| Some (module_id, _contents_or_filename) ->
module_id_contents := StringMap.add module_id "" !module_id_contents

let usage_msg = "DkStd_Std.Project.Init [-verbose] [-windows-boot] [-delete-dkcoder-after] NEW_PROJECT_DIR DKCODER_PROJECT_DIR"
let usage_msg = "DkStd_Std.Project.Init [-verbose] [-windows-boot] [-delete-dkcoder-after] NEW_PROJECT_DIR DKCODER_PROJECT_DIR [MODULE_ID1=] [MODULE_ID2=] ..."
let speclist =
[
("-verbose", Arg.Set verbose, "Output debug information.");
Expand Down Expand Up @@ -186,11 +198,29 @@ let () =
(* Use CRLF on Windows since .json are "*.json text" in .gitattributes *)
write_crlf_on_win32 settings_json (String.trim contents_settings_json_untrimmed));

(* src/**.ml *)
let source_files = List.filter_map (fun (module_id, content) ->
let ml_file =
let src_mod = "src" :: Stringext.split ~on:'.' module_id in
(String.concat "/" src_mod) ^ ".ml"
in
match Fpath.of_string ml_file with
| Error (`Msg msg) -> Printf.eprintf "dkcoder: INVALID module id: %s. %s\n%!" module_id msg; exit 4
| Ok fp ->
let abs_fp = Fpath.(new_project_dirp // fp) in
Bos.OS.Dir.create (Fpath.parent abs_fp) |> Utils.rmsg |> ignore;
Bos.OS.File.write abs_fp content |> Utils.rmsg;
Some ml_file)
(StringMap.bindings !module_id_contents)
in

(* git add, git update-index *)
let project_files = ["dk"; "dk.cmd"; "__dk.cmake";
".gitattributes"; ".gitignore";
".ocamlformat";
".vscode/settings.json"; ".vscode/extensions.json"] in
".vscode/settings.json"; ".vscode/extensions.json"]
@ source_files
in
Utils.git ~quiet:() ~slots ("add" :: project_files);
Utils.git ~quiet:() ~slots ["update-index"; "--chmod=+x"; "dk"];

Expand Down

0 comments on commit d434329

Please sign in to comment.