@@ -60,6 +60,7 @@ fn printHelp() void {
6060 \\ cljw -P Resolve deps only
6161 \\ cljw build <file> [-o out] Build standalone binary
6262 \\ cljw test [files...] Run tests
63+ \\ cljw new <name> Create new project
6364 \\
6465 \\deps.edn flags:
6566 \\ -A:alias[:alias...] Apply alias(es) for REPL
@@ -91,6 +92,7 @@ fn printHelp() void {
9192 \\ cljw -X:build my.ns/task Exec function with :build alias
9293 \\ cljw -P Fetch git dependencies
9394 \\ cljw build app.clj -o app Build standalone binary
95+ \\ cljw new my-app Create new project
9496 \\
9597 ) catch {};
9698}
@@ -180,6 +182,12 @@ pub fn main() !void {
180182 return ;
181183 }
182184
185+ // Handle `new` subcommand: cljw new <project-name>
186+ if (args .len >= 2 and std .mem .eql (u8 , args [1 ], "new" )) {
187+ handleNewCommand (args [2.. ]);
188+ return ;
189+ }
190+
183191 // Parse flags
184192 const CliMode = enum { normal , alias_repl , main_mode , exec_mode , resolve_only , show_path };
185193 var mode : CliMode = .normal ;
@@ -1134,6 +1142,115 @@ fn checkTestFailures(result: Value) bool {
11341142 return false ;
11351143}
11361144
1145+ /// Handle `cljw new <project-name>` subcommand.
1146+ /// Creates a new project directory with deps.edn, src/, and test/ scaffolding.
1147+ fn handleNewCommand (new_args : []const [:0 ]const u8 ) void {
1148+ const stderr : std.fs.File = .{ .handle = std .posix .STDERR_FILENO };
1149+ const stdout : std.fs.File = .{ .handle = std .posix .STDOUT_FILENO };
1150+
1151+ if (new_args .len == 0 ) {
1152+ _ = stderr .write ("Usage: cljw new <project-name>\n " ) catch {};
1153+ std .process .exit (1 );
1154+ }
1155+
1156+ const project_name = new_args [0 ];
1157+
1158+ // Validate project name (alphanumeric, hyphens, underscores)
1159+ for (project_name ) | c | {
1160+ if (! std .ascii .isAlphanumeric (c ) and c != '-' and c != '_' and c != '.' ) {
1161+ _ = stderr .write ("Error: invalid project name (use alphanumeric, hyphens, underscores)\n " ) catch {};
1162+ std .process .exit (1 );
1163+ }
1164+ }
1165+
1166+ // Convert project name to namespace: hyphens -> underscores for dirs/files
1167+ var ns_name_buf : [256 ]u8 = undefined ;
1168+ const ns_name = blk : {
1169+ if (project_name .len > ns_name_buf .len ) {
1170+ _ = stderr .write ("Error: project name too long\n " ) catch {};
1171+ std .process .exit (1 );
1172+ }
1173+ @memcpy (ns_name_buf [0.. project_name .len ], project_name );
1174+ // Namespace uses hyphens (Clojure convention), file path uses underscores
1175+ break :blk ns_name_buf [0.. project_name .len ];
1176+ };
1177+
1178+ var file_name_buf : [256 ]u8 = undefined ;
1179+ const file_name = blk : {
1180+ @memcpy (file_name_buf [0.. project_name .len ], project_name );
1181+ // Replace hyphens with underscores for file paths
1182+ for (file_name_buf [0.. project_name .len ]) | * c | {
1183+ if (c .* == '-' ) c .* = '_' ;
1184+ }
1185+ break :blk file_name_buf [0.. project_name .len ];
1186+ };
1187+
1188+ // Create project directory
1189+ std .fs .cwd ().makeDir (project_name ) catch | e | {
1190+ if (e == error .PathAlreadyExists ) {
1191+ _ = stderr .write ("Error: directory already exists\n " ) catch {};
1192+ } else {
1193+ _ = stderr .write ("Error creating directory\n " ) catch {};
1194+ }
1195+ std .process .exit (1 );
1196+ };
1197+
1198+ var project_dir = std .fs .cwd ().openDir (project_name , .{}) catch {
1199+ _ = stderr .write ("Error: cannot open project directory\n " ) catch {};
1200+ std .process .exit (1 );
1201+ };
1202+ defer project_dir .close ();
1203+
1204+ // Create subdirectories
1205+ project_dir .makePath ("src" ) catch {};
1206+ project_dir .makePath ("test" ) catch {};
1207+
1208+ // Write deps.edn
1209+ {
1210+ var buf : [1024 ]u8 = undefined ;
1211+ var stream = std .io .fixedBufferStream (& buf );
1212+ const w = stream .writer ();
1213+ w .print ("{{:paths [\" src\" ]\n :deps {{}}\n :aliases\n {{:test {{:extra-paths [\" test\" ]}}}}}}\n " , .{}) catch {};
1214+ project_dir .writeFile (.{ .sub_path = "deps.edn" , .data = stream .getWritten () }) catch {};
1215+ }
1216+
1217+ // Write src/<file_name>.clj
1218+ {
1219+ var path_buf : [512 ]u8 = undefined ;
1220+ var path_stream = std .io .fixedBufferStream (& path_buf );
1221+ path_stream .writer ().print ("src/{s}.clj" , .{file_name }) catch {};
1222+ const src_path = path_stream .getWritten ();
1223+
1224+ var buf : [1024 ]u8 = undefined ;
1225+ var stream = std .io .fixedBufferStream (& buf );
1226+ const w = stream .writer ();
1227+ w .print ("(ns {s})\n\n (defn -main [& args]\n (println \" Hello from {s}!\" ))\n " , .{ ns_name , ns_name }) catch {};
1228+ project_dir .writeFile (.{ .sub_path = src_path , .data = stream .getWritten () }) catch {};
1229+ }
1230+
1231+ // Write test/<file_name>_test.clj
1232+ {
1233+ var path_buf : [512 ]u8 = undefined ;
1234+ var path_stream = std .io .fixedBufferStream (& path_buf );
1235+ path_stream .writer ().print ("test/{s}_test.clj" , .{file_name }) catch {};
1236+ const test_path = path_stream .getWritten ();
1237+
1238+ var buf : [1024 ]u8 = undefined ;
1239+ var stream = std .io .fixedBufferStream (& buf );
1240+ const w = stream .writer ();
1241+ w .print ("(ns {s}-test\n (:require [clojure.test :refer [deftest is testing run-tests]]\n [{s} :refer :all]))\n\n (deftest greeting-test\n (testing \" main function\" \n (is (= 1 1))))\n\n (run-tests)\n " , .{ ns_name , ns_name }) catch {};
1242+ project_dir .writeFile (.{ .sub_path = test_path , .data = stream .getWritten () }) catch {};
1243+ }
1244+
1245+ {
1246+ var msg_buf : [1024 ]u8 = undefined ;
1247+ var msg_stream = std .io .fixedBufferStream (& msg_buf );
1248+ const w = msg_stream .writer ();
1249+ w .print ("Project '{s}' created!\n\n cd {s}\n cljw -M -m {s} # Run main\n cljw test # Run tests\n cljw # Start REPL\n " , .{ project_name , project_name , ns_name }) catch {};
1250+ _ = stdout .write (msg_stream .getWritten ()) catch {};
1251+ }
1252+ }
1253+
11371254/// Recursively collect .clj files from a directory.
11381255/// str_alloc: arena for path strings (long-lived), list_alloc: for ArrayList backing.
11391256fn collectTestFiles (str_alloc : Allocator , list_alloc : Allocator , dir_path : []const u8 , out : * std .ArrayList ([]const u8 )) void {
0 commit comments