generate the requires list and docstrings via nix and unit module

This commit is contained in:
Sameer Rahmani 2024-04-15 18:57:22 +01:00
parent 74d36d5ae1
commit 92549b9a08
Signed by: lxsameer
GPG Key ID: 8741FACBF412FFA5
5 changed files with 89 additions and 49 deletions

View File

@ -26,6 +26,8 @@
;;
;; Please note that this file runs with Emacs and not FG42.
;;; Code:
(require 'loaddefs-gen)
(when (not (json-available-p))
(error "Error: libjasson support is missing"))
@ -60,6 +62,9 @@
;; ============================================================================
;; This file is auto generated by FG42. Don't modify it.
;; ============================================================================
;; Since it is deprecated we can easily avoide other packages to use it
(provide 'cl)
")
(defvar file-footer "
@ -68,17 +73,21 @@
;; fg42_init.el ends here
")
(defvar keys-to-skip '("docstrings" "mimeTypes"))
;; Elisp's hashtables are not functional so I have to store the
;; state like this, Ewwwwww!
(defvar build-state '()
"A list of expressions that will dump in the output at the end.")
(defvar build-output (cadr command-line-args-left))
;;; Build related stuff
(defmacro with-json (var &rest body)
"Read the json file from cli arguments, user VAR run the BODY on it."
(declare (indent defun))
`(let ((input-file (car command-line-args-left)))
(message "Reading the input file: %s" input-file)
(message "[build.el]: Reading the input file: %s" input-file)
(with-temp-buffer
(insert-file-contents input-file)
(let ((,var (json-parse-buffer)))
@ -94,24 +103,29 @@
,@(mapcar (lambda (pkg) `(require ',(intern pkg))) elisp-pkgs)))))
(defun generate-generic-vars (k v)
"Generate a list of vars for K and value V."
(add-to-list 'build-state `(defvar ,(intern (format "fg42/config/%s" k)) ,v)))
(defun generate-generic-vars (k v doc)
"Generate a list of vars for K and value V with docstring DOC."
(add-to-list 'build-state `(defvar ,(intern (format "fg42/config/%s" k)) ,v ,doc)))
(defun fg42-config-key-handler (k v)
"Handle the FG42 config K and its value V."
(cond
((string= k "requires")
(generate-compile-time-requires v))
(t (generate-generic-vars k v))))
(defun fg42-config-key-handler (config)
"Generate a lambda to process key/value pairs in CONFIG."
(lambda (k v)
"Handle the FG42 config K and its value V."
(cond
((string= k "requires")
(generate-compile-time-requires v))
((member k keys-to-skip)
(message "[build.el]: Skipping %s..." k))
(t (generate-generic-vars k v (gethash k (gethash "docstrings" config)))))))
(defun fg42-handler (config)
"Handle the CONFIG for FG42.
CONFIG maps to the collective `config' value of Nix modules."
(maphash #'fg42-config-key-handler config))
(maphash (fg42-config-key-handler config) config))
(defun handle-conifg-for (k v)
"Find the handler of K and its value V to it."
@ -124,15 +138,17 @@ CONFIG maps to the collective `config' value of Nix modules."
(error "Don't know how to handle '%s'" k))
(handle-conifg-for k v))
(with-json j
;; TODO: To make it scale, may be let Nix modules
;; plug into this script. There is a security
;; rist though
(maphash #'handle-top-level j)
(with-temp-file (cadr command-line-args-left)
(with-temp-file build-output
(insert file-header)
(mapc (lambda (x) (print x (current-buffer))) build-state)
(insert file-footer)))
(insert file-footer))
(message "[build.el]: Done!"))
(provide 'build)

View File

@ -18,8 +18,6 @@
# from FG42 modules that are structurally the same but used in
# different context
{ pkgs, lib ? pkgs.lib, modules ? null, extraModules ? [ ] }:
with lib;
let
fg42Modules =
if modules != null
@ -33,9 +31,8 @@ let
utils = pkgs.callPackage ./utils.nix { };
unitModule = import ./unit.nix { inherit pkgs lib; };
final = lib.evalModules {
modules = [ unitModule ] ++ fg42Modules ++ extraModules;
modules = [ ./unit.nix ] ++ fg42Modules ++ extraModules;
specialArgs = {
inherit utils;
inherit (utils) makeFG42Drv;

View File

@ -86,8 +86,8 @@ stdenv.mkDerivation rec {
chmod 755 $LISPDIR -R
emacs --batch -l $LISPDIR/lisp/build.el $LISPDIR/config.json $STARTUP_FILE
#emacs -Q --batch -l package --eval "(package-generate-autoloads \"fg42_init" \"$LISPDIR\")"
emacs --batch -l loaddefs-gen -f loaddefs-generate-batch $LISPDIR/lisp/ $LISPDIR/lisp/ ${emacsBundle}
cat >> $out/share/applications/FG42.desktop << EOF
[Desktop Entry]

View File

@ -17,8 +17,11 @@
# This is the home manager module that exposes FG42. It differs
# from FG42 modules that are structurally the same but used in
# different context
{ lib, pkgs, ... }:
{ lib, pkgs, config, options, ... }:
with lib;
let
nix2elispName = pkg: (builtins.replaceStrings [ "fg42-" ] [ "fg42/" ] pkg.pname);
in
{
options = {
fg42.version = mkOption {
@ -44,17 +47,6 @@ with lib;
'';
};
fg42.requires = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
A list of Emacs packages to preload in compile time.
In general you want your entry point module in this list
to setup your autoloads, hooks, and everything.
'';
};
fg42.paths = mkOption {
type = types.listOf types.package;
default = [ ];
@ -91,6 +83,7 @@ with lib;
type = types.str;
description = "The theme name to use with FG42.";
};
fg42.theme-package-name = mkOption
{
type = types.str;
@ -100,10 +93,46 @@ with lib;
'';
};
fg42.requires = mkOption {
type = types.listOf types.str;
readOnly = true;
description = ''
A list of Emacs packages to preload in compile time.
In general you want your entry point module in this list
to setup your autoloads, hooks, and everything.
'';
};
fg42.docstrings = mkOption {
type = types.attrsOf types.str;
readOnly = true;
description = ''
A list of Emacs packages to preload in compile time.
In general you want your entry point module in this list
to setup your autoloads, hooks, and everything.
'';
};
};
config = {
fg42.version = import ../version.nix { };
# Extract the package pname's to pass to elisp to `require` them in the init
# file, while compiling.
fg42.requires = map nix2elispName config.fg42.elispPackages;
fg42.docstrings = (with builtins;
let
desc = opt:
if hasAttr "description" opt
then opt.description
else "";
in
lib.concatMapAttrs
(k: v: { ${k} = desc v; })
options.fg42);
};
}

View File

@ -41,7 +41,7 @@ let
base16-theme
]);
drv = makeFG42Drv {
pname = "fg42-editor";
pname = "editor";
version = config.fg42.version;
buildInputs = deps;
src = ./.;
@ -50,23 +50,21 @@ let
dicts = pkgs.aspellWithDicts (dicts: with dicts; [ en en-computers en-science ]);
in
{
config = {
fg42.elispPackages = [ drv ] ++ deps;
fg42.elispPackages = [ drv ] ++ deps;
fg42.fonts = (with pkgs;[
vazir-fonts
fira-code
fira-mono
noto-fonts
]);
fg42.fonts = (with pkgs;[
vazir-fonts
fira-code
fira-mono
noto-fonts
]);
fg42.paths = (with pkgs;[
ripgrep
dicts
]);
fg42.paths = (with pkgs;[
ripgrep
dicts
]);
fg42.font = lib.mkDefault '''("Fira Mono" 11)'';
fg42.theme = lib.mkDefault "base16-eighties";
fg42.theme-package-name = lib.mkDefault "base16-theme";
};
fg42.font = lib.mkDefault '''("Fira Mono" 11)'';
fg42.theme = lib.mkDefault "base16-eighties";
fg42.theme-package-name = lib.mkDefault "base16-theme";
}