@@ -1244,6 +1244,89 @@ of the term to replace."
1244
1244
(find-file (car files )))
1245
1245
(t (find-file (completing-read " Package file: " files nil t ))))))
1246
1246
1247
+ (defun idris-start-idris2-project ()
1248
+ " Interactively create a new Idris2 project with ipkg file and first module."
1249
+ (interactive )
1250
+ (cl-flet ((project-name ()
1251
+ (let ((project-name (string-trim (read-string " Project name: " ))))
1252
+ (when (string-match-p " [^a-zA-Z0-9_ ]" project-name)
1253
+ (user-error " Project name should consist only of letters, numbers, spaces and underscores" ))
1254
+ (when (string= " " project-name)
1255
+ (user-error " Project name can not be empty" ))
1256
+ project-name))
1257
+ (project-directory (default-filename)
1258
+ (let ((dir (read-directory-name " Create in: " nil default-filename nil default-filename)))
1259
+ (when (string= " " dir)
1260
+ (user-error " Project directory can not be empty" ))
1261
+ (when (file-exists-p dir)
1262
+ (user-error " %s already exists" dir))
1263
+ dir)))
1264
+ (let* ((project-name (project-name))
1265
+ (default-filename (downcase (replace-regexp-in-string " [^a-zA-Z0-9_-]" " -" project-name)))
1266
+ (package-name default-filename)
1267
+ (create-in (project-directory default-filename))
1268
+ (src-dir (string-trim (read-string " Source directory (src): " nil nil " src" )))
1269
+ (authors (string-trim (read-string (format " Authors (%s ): " (user-full-name )) nil nil (user-full-name ))))
1270
+ (options (string-trim (read-string " Options: " )))
1271
+ (module-name-suggestion (replace-regexp-in-string " [^a-zA-Z0-9]+" " ." (capitalize project-name)))
1272
+ (first-mod (string-trim (read-string
1273
+ (format " First module name (%s ): " module-name-suggestion)
1274
+ nil nil module-name-suggestion)))
1275
+ (ipkg-file (file-truename (concat (file-name-as-directory create-in)
1276
+ (concat default-filename " .ipkg" ))))
1277
+ (output-buffer (generate-new-buffer " *Idris Script Output*" ))
1278
+ (input-buffer (generate-new-buffer " *Idris Script Input*" )))
1279
+
1280
+ (make-directory (concat (file-name-as-directory create-in) src-dir) t )
1281
+ (with-current-buffer input-buffer
1282
+ (insert package-name) (newline )
1283
+ (insert authors) (newline )
1284
+ (insert options) (newline )
1285
+ (insert src-dir) (newline )
1286
+
1287
+ (call-process-region (point-min ) (point-max )
1288
+ idris-interpreter-path
1289
+ nil
1290
+ output-buffer
1291
+ nil
1292
+ " --init"
1293
+ ipkg-file))
1294
+ (let ((output (with-current-buffer output-buffer
1295
+ (buffer-string ))))
1296
+ (when (string-match-p " error" output)
1297
+ (message " Idris: %s " output)))
1298
+
1299
+ (kill-buffer output-buffer)
1300
+ (kill-buffer input-buffer)
1301
+
1302
+ ; ; Decorate the generated ipkg file
1303
+ (when (file-exists-p ipkg-file)
1304
+ (save-excursion
1305
+ (find-file ipkg-file)
1306
+ (goto-char (point-min ))
1307
+ (insert " -- " project-name) (newline ) (newline )
1308
+ (when (re-search-forward " ^-- version =" nil t )
1309
+ (replace-match " version = 0.1.0" ))
1310
+ (when (and (not (string= first-mod " " ))
1311
+ (re-search-forward " ^-- modules =" nil t ))
1312
+ (replace-match " modules = " )
1313
+ (insert first-mod))
1314
+ (save-buffer )
1315
+
1316
+ ; ; Create Idris file in the source directory of the project
1317
+ ; ; Default directory is the project directry
1318
+ (when (not (string= first-mod " " ))
1319
+ (let* ((mod-path (reverse (split-string first-mod " \\ .+" )))
1320
+ (mod-dir (mapconcat #'file-name-as-directory
1321
+ (cons src-dir (reverse (cdr mod-path)))
1322
+ " " ))
1323
+ (mod-filename (concat mod-dir (car mod-path) " .idr" )))
1324
+ (make-directory mod-dir t )
1325
+ (pop-to-buffer (find-file-noselect mod-filename))
1326
+ (insert " module " first-mod) (newline ) (newline )
1327
+ (insert " %default total" ) (newline )
1328
+ (save-buffer ))))))))
1329
+
1247
1330
(defun idris-start-project ()
1248
1331
" Interactively create a new Idris project, complete with ipkg file."
1249
1332
(interactive )
0 commit comments