Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Config defaults for dune-project #10835

Merged
merged 18 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ let shared_with_config_file =
; cache_storage_mode
; action_stdout_on_success
; action_stderr_on_success
; project_defaults = None
; experimental = None
}
;;
Expand Down
22 changes: 18 additions & 4 deletions bin/dune_init.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,15 @@ end

(** The context in which the initialization is executed *)
module Init_context = struct
open Dune_config_file

type t =
{ dir : Path.t
; project : Dune_project.t
; defaults : Dune_config.Project_defaults.t
}

let make path =
let make path defaults =
let open Memo.O in
let+ project =
(* CR-rgrinberg: why not get the project from the source tree? *)
Expand All @@ -196,7 +199,7 @@ module Init_context = struct
| Some p -> Path.of_string p
in
File.create_dir dir;
{ dir; project }
{ dir; project; defaults }
;;
end

Expand Down Expand Up @@ -373,7 +376,12 @@ module Component = struct
let test common (() : Options.Test.t) = make "test" common []

(* A list of CSTs for dune-project file content *)
let dune_project ~opam_file_gen dir (common : Options.Common.t) =
let dune_project
~opam_file_gen
~(defaults : Dune_config_file.Dune_config.Project_defaults.t)
dir
(common : Options.Common.t)
=
let cst =
let package =
Package.create
Expand All @@ -400,7 +408,12 @@ module Component = struct
]
in
let packages = Package.Name.Map.singleton (Package.name package) package in
let info = Package_info.example in
let info =
Package_info.example
~authors:defaults.authors
~maintainers:defaults.maintainers
~license:defaults.license
in
Dune_project.anonymous ~dir info packages
|> Dune_project.set_generate_opam_files opam_file_gen
|> Dune_project.encode
Expand Down Expand Up @@ -476,6 +489,7 @@ module Component = struct
let content =
Stanza_cst.dune_project
~opam_file_gen
~defaults:context.defaults
Path.(as_in_source_tree_exn context.dir)
common
in
Expand Down
5 changes: 4 additions & 1 deletion bin/dune_init.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ open Import

(** The context in which the initialization is executed *)
module Init_context : sig
open Dune_config_file

type t =
{ dir : Path.t
; project : Dune_project.t
; defaults : Dune_config.Project_defaults.t
}

val make : string option -> t Memo.t
val make : string option -> Dune_config.Project_defaults.t -> t Memo.t
end

module Public_name : sig
Expand Down
7 changes: 5 additions & 2 deletions bin/init.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ let context_cwd : Init_context.t Term.t =
and+ path = path in
let builder = Common.Builder.set_default_root_is_cwd builder true in
let common, config = Common.init builder in
Scheduler.go ~common ~config (fun () -> Memo.run (Init_context.make path))
let project_defaults = config.project_defaults in
Scheduler.go ~common ~config (fun () ->
Memo.run (Init_context.make path project_defaults))
;;

module Public_name = struct
Expand Down Expand Up @@ -228,7 +230,8 @@ let project =
let builder = Builder.set_root common_builder root in
let (_ : Fpath.mkdir_p_result) = Fpath.mkdir_p root in
let common, config = Common.init builder in
Scheduler.go ~common ~config (fun () -> Memo.run init_context)
let project_defaults = config.project_defaults in
Scheduler.go ~common ~config (fun () -> Memo.run @@ init_context project_defaults)
in
Component.init
(Project { context; common; options = { template; inline_tests; pkg } });
Expand Down
3 changes: 3 additions & 0 deletions doc/changes/10835.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Add support for specifying default values of the `authors`, `maintainers`, and
`license` stanzas of the `dune-project` file via the dune config file. Default
values are set using the `(project_defaults)` stanza (#10835, @H-ANSEN)
1 change: 1 addition & 0 deletions doc/reference/config/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ The ``config`` file can contain the following stanzas:
cache_storage_mode
display
jobs
project_defaults
sandboxing_preference
terminal_persistence
53 changes: 53 additions & 0 deletions doc/reference/config/project_defaults.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
project_defaults
----------------

.. versionadded:: 3.17

Specify default values for stanzas ``authors``, ``maintainers``, and ``license``
of the :doc:`../dune-project/index` file when initializing a project with
``dune init proj``. The format of the 'project_defaults' stanza is as follows:

.. code:: dune

(project_defaults
<optional-fields>)

``<optional-fields>`` are:

.. describe:: (authors <string(s)>)

Specify authors.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, if this could link to the definition of authors in the dune-project reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any relevant definition of authors in the reference, I'm sure I am just glancing over it though. I made all the other requested changes as well as adding a .. versionadded 3.17 to the top of the doc page!


Example:

.. code:: dune

(project_defaults
(authors
"Jane Doe <jane.doe@example.com>"
"John Doe <john.doe@example.com>"))

.. describe:: (maintainers <string(s)>)

Specify maintainers.

Example:

.. code:: dune

(project_defaults
(maintainers
"Jane Doe <jane.doe@example.com>"
"John Doe <john.doe@example.com>"))

.. describe:: (license <string(s)>)

Specify license, ideally as an identifier from the `SPDX License List
<https://spdx.org/licenses/>`__.

Example:

.. code:: dune

(project_defaults
(license "MIT"))
34 changes: 34 additions & 0 deletions src/dune_config_file/dune_config_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ module Dune_config = struct
simplicity *)
let syntax = Stanza.syntax

module Project_defaults = struct
type t =
{ authors : string list option
H-ANSEN marked this conversation as resolved.
Show resolved Hide resolved
; maintainers : string list option
; license : string list option
}

let decode =
fields
(let+ authors = field_o "authors" (repeat string)
and+ maintainers = field_o "maintainers" (repeat string)
and+ license = field_o "license" (repeat string) in
{ authors; maintainers; license })
;;

let to_dyn t =
let f = Dyn.(option (list string)) in
Dyn.record
[ "authors", f t.authors; "maintainers", f t.maintainers; "license", f t.license ]
;;
end

module Terminal_persistence = struct
type t =
| Preserve
Expand Down Expand Up @@ -136,6 +158,7 @@ module Dune_config = struct
; cache_storage_mode : Cache.Storage_mode.t field
; action_stdout_on_success : Action_output_on_success.t field
; action_stderr_on_success : Action_output_on_success.t field
; project_defaults : Project_defaults.t field
; experimental : (string * (Loc.t * string)) list field
}
end
Expand Down Expand Up @@ -163,6 +186,7 @@ module Dune_config = struct
field a.action_stdout_on_success b.action_stdout_on_success
; action_stderr_on_success =
field a.action_stderr_on_success b.action_stderr_on_success
; project_defaults = field a.project_defaults b.project_defaults
; experimental = field a.experimental b.experimental
}
;;
Expand All @@ -186,6 +210,7 @@ module Dune_config = struct
; cache_storage_mode
; action_stdout_on_success
; action_stderr_on_success
; project_defaults
; experimental
}
=
Expand All @@ -205,6 +230,7 @@ module Dune_config = struct
, field Action_output_on_success.to_dyn action_stdout_on_success )
; ( "action_stderr_on_success"
, field Action_output_on_success.to_dyn action_stderr_on_success )
; "project_defaults", field Project_defaults.to_dyn project_defaults
; ( "experimental"
, field Dyn.(list (pair string (fun (_, v) -> string v))) experimental )
]
Expand All @@ -228,6 +254,7 @@ module Dune_config = struct
; cache_storage_mode = None
; action_stdout_on_success = None
; action_stderr_on_success = None
; project_defaults = None
; experimental = None
}
;;
Expand Down Expand Up @@ -294,6 +321,11 @@ module Dune_config = struct
; cache_storage_mode = Some (Dune_cache_storage.Mode.default ())
; action_stdout_on_success = Print
; action_stderr_on_success = Print
; project_defaults =
{ authors = Some [ "Author Name <author@example.com>" ]
; maintainers = Some [ "Maintainer Name <maintainer@example.com>" ]
; license = Some [ "LICENSE" ]
}
; experimental = []
}
;;
Expand Down Expand Up @@ -357,6 +389,7 @@ module Dune_config = struct
field_o "action_stdout_on_success" (3, 0) Action_output_on_success.decode
and+ action_stderr_on_success =
field_o "action_stderr_on_success" (3, 0) Action_output_on_success.decode
and+ project_defaults = field_o "project_defaults" (3, 17) Project_defaults.decode
and+ experimental =
field_o "experimental" (3, 8) (repeat (pair string (located string)))
in
Expand All @@ -377,6 +410,7 @@ module Dune_config = struct
; cache_storage_mode
; action_stdout_on_success
; action_stderr_on_success
; project_defaults
; experimental
}
;;
Expand Down
11 changes: 11 additions & 0 deletions src/dune_config_file/dune_config_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ module Dune_config : sig
open Dune_config
module Display : module type of Display

module Project_defaults : sig
type t =
{ authors : string list option
; maintainers : string list option
; license : string list option
}

val decode : t Dune_lang.Decoder.t
end

module Concurrency : sig
type t =
| Fixed of int
Expand Down Expand Up @@ -56,6 +66,7 @@ module Dune_config : sig
; cache_storage_mode : Cache.Storage_mode.t field
; action_stdout_on_success : Action_output_on_success.t field
; action_stderr_on_success : Action_output_on_success.t field
; project_defaults : Project_defaults.t field
; experimental : (string * (Loc.t * string)) list field
}
end
Expand Down
10 changes: 6 additions & 4 deletions src/dune_lang/package_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ let empty =
}
;;

let example =
let example ~authors ~maintainers ~license =
{ source =
Some (Host (Source_kind.Host.Github { user = "username"; repo = "reponame" }))
; license = Some [ "LICENSE" ]
; authors = Some [ "Author Name <author@example.com>" ]
; maintainers = Some [ "Maintainer Name <maintainer@example.com>" ]
; license = Some (Option.value license ~default:[ "LICENSE" ])
; authors = Some (Option.value authors ~default:[ "Author Name <author@example.com>" ])
; maintainers =
Some
(Option.value maintainers ~default:[ "Maintainer Name <maintainer@example.com>" ])
; documentation =
Some "https://url/to/documentation"
(* homepage and bug_reports are inferred from the source *)
Expand Down
6 changes: 5 additions & 1 deletion src/dune_lang/package_info.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ val documentation : t -> string option
val maintainers : t -> string list option

(** example package info (used for project initialization ) *)
val example : t
val example
: authors:string list option
-> maintainers:string list option
-> license:string list option
-> t

val empty : t
val to_dyn : t Dyn.builder
Expand Down
Loading
Loading