Skip to content

Commit

Permalink
Add support for automatically gitignoring generated files
Browse files Browse the repository at this point in the history
Add a light `gitignore` abstraction allowing to define a set of
gitignore patterns from Nickel, and plug that in to the file generation
interface so that a generated file can also be git ignored by simply
setting `files.foo.gitignore = true`
  • Loading branch information
Théophane Hufschmitt committed Oct 17, 2023
1 parent 98b4641 commit 2b7ebe9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
##########
# This file is autogenerated by Organist, don't edit
##########
.editorconfig
/examples/*/result
/result
/result
52 changes: 52 additions & 0 deletions lib/git.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# let nix = import "./nix-interop/nix.ncl" in
let filegen = import "./files.ncl" in
let Gitignorable = {
gitignore
: Bool
| doc "Whether to gitignore the file"
| default
= false,
..
}
in
let header : String = m%"
##########
# This file is autogenerated by Organist, don't edit
##########

"%
in
let ignored_files | { _ : Gitignorable } -> { _ : Bool } = fun files =>
files
|> std.record.to_array
|> std.array.filter (fun { value = { gitignore, .. }, .. } => gitignore)
|> std.array.map (fun { value = { target, .. }, .. } => { field = target, value = true })
|> std.record.from_array
in
let gitignore_content | { _ : Bool } -> String = fun ignores =>
ignores
|> std.record.map
(
fun pattern is_ignored =>
"%{if is_ignored then "" else "!"}%{pattern}"
)
|> std.record.values
|> std.string.join "\n"
|> (fun c => header ++ c ++ "\n")
in
{
Schema =
filegen.Schema
& {

files | filegen.Files,
files | { _ : Gitignorable },

git.ignore
| { _ : Bool }
= ignored_files files,

files.".gitignore".materialisation_method = 'Copy,
files.".gitignore".content = gitignore_content git.ignore,
}
}
2 changes: 2 additions & 0 deletions lib/schema.ncl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let nix = import "./nix-interop/nix.ncl" in
let filegen = import "files.ncl" in
let git = import "git.ncl" in
{
OrganistShells = {
dev | nix.derivation.NickelDerivation = build,
Expand Down Expand Up @@ -30,4 +31,5 @@ let filegen = import "files.ncl" in
..
}
& filegen.Schema
& git.Schema,
}
19 changes: 14 additions & 5 deletions project.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,21 @@ let import_nix = organist.nix.import_nix in

flake.checks = import "tests/main.ncl",

files.".gitignore".materialisation_method = 'Copy,
files.".gitignore".content = m%"
# This file is generated by Organist, please don't edit directly
/examples/*/result
/result
# Simple config file. It will be gitignore and regenerated on the fly by the
# `#regenerate-files` command.
files.".editorconfig".content = m%"
# Auto-generated by Organist, don't edit

root = true
[*.{nix,nickel}]
indent_style = spaces
indent_size = 2

"%,
files.".editorconfig".gitignore = true,

# Extra gitignored files
git.ignore."/result" = true,
git.ignore."/examples/*/result" = true,
}
| organist.OrganistExpression

0 comments on commit 2b7ebe9

Please sign in to comment.