Skip to content

init

Set config files for somesy.

initialize

initialize(
    ctx: Context,
    output_file: Path = typer.Option(
        Path("somesy.toml"),
        "--output-file",
        "-o",
        help="Path for the generated somesy.toml file (default: somesy.toml).",
        **file_arg_config
    ),
    overwrite: bool = typer.Option(False, "--overwrite"),
    non_interactive: bool = typer.Option(
        False,
        "--non-interactive",
        help="Do not prompt for missing metadata; report warnings instead.",
    ),
)

Harvest project metadata and create a somesy.toml file.

Source code in src/somesy/cli/init.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
@app.callback(invoke_without_command=True)
@wrap_exceptions
def initialize(
    ctx: typer.Context,
    output_file: Path = typer.Option(
        Path("somesy.toml"),
        "--output-file",
        "-o",
        help="Path for the generated somesy.toml file (default: somesy.toml).",
        **file_arg_config,
    ),
    overwrite: bool = typer.Option(False, "--overwrite"),
    non_interactive: bool = typer.Option(
        False,
        "--non-interactive",
        help="Do not prompt for missing metadata; report warnings instead.",
    ),
):
    """Harvest project metadata and create a somesy.toml file."""
    if ctx.invoked_subcommand is not None:
        return
    root = Path.cwd()
    sources = harvest_sources(root)
    git_metadata = harvest_git(root)
    fallback = _prompt_missing_metadata(sources, git_metadata, non_interactive)
    source_content = [content for _, content in sources]
    if fallback:
        source_content.append(fallback)
    metadata = merge_metadata(
        source_content, git_metadata, allow_incomplete=non_interactive
    )
    source_names = {path.name for path, _ in sources}
    incomplete = (
        any(
            getattr(metadata, field, None) is None
            for field in ("name", "description", "license")
        )
        or not metadata.authors()
    )
    config = None
    if source_names or incomplete:
        config_data = {
            f"no_sync_{key}": filename not in source_names
            for key, filename in {
                "pyproject": "pyproject.toml",
                "package_json": "package.json",
                "julia": "Project.toml",
                "fortran": "fpm.toml",
                "pom_xml": "pom.xml",
                "mkdocs": "mkdocs.yml",
                "rust": "Cargo.toml",
            }.items()
        }
        config = SomesyConfig.model_validate(config_data)
    output = output_file if output_file.is_absolute() else root / output_file
    write_somesy_file(metadata, output, config=config, overwrite=overwrite)
    typer.echo(f"Created {output}")

config

config()

Set CLI configs for somesy.

Source code in src/somesy/cli/init.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
@app.command()
@wrap_exceptions
def config():
    """Set CLI configs for somesy."""
    # check if input file exists, if not, try to find it from default list
    input_file_default = discover_input()

    # prompt for inputs
    input_file = Path(typer.prompt("Input file path", default=input_file_default))
    options: dict[str, Any] = {"input_file": Path(input_file)}

    # ----

    options["no_sync_cff"] = not typer.confirm(
        "Do you want to sync to a CFF file?", default=True
    )
    if cff_file := typer.prompt("CFF file path", default="CITATION.cff"):
        options["cff_file"] = cff_file

    options["no_sync_codemeta"] = not typer.confirm(
        "Do you want to sync to a codemeta.json file?", default=True
    )
    if codemeta_file := typer.prompt(
        "codemeta.json file path", default="codemeta.json"
    ):
        options["codemeta_file"] = codemeta_file

    options["no_sync_pyproject"] = not typer.confirm(
        "Do you want to sync to a pyproject.toml file?", default=True
    )
    if pyproject_file := typer.prompt(
        "pyproject.toml file path", default="pyproject.toml"
    ):
        options["pyproject_file"] = pyproject_file

    options["sync_package_json"] = typer.confirm(
        "Do you want to sync to a package.json file?", default=False
    )
    if package_json_file := typer.prompt(
        "package.json file path", default="package.json"
    ):
        options["package_json_file"] = package_json_file

    options["no_sync_julia"] = not typer.confirm(
        "Do you want to sync to a Project.toml(Julia) file?", default=True
    )
    if julia_file := typer.prompt(
        "Project.toml (Julia) file path", default="Project.toml"
    ):
        options["julia_file"] = julia_file

    options["no_sync_fortran"] = not typer.confirm(
        "Do you want to sync to a fpm.toml(fortran) file?", default=True
    )
    fortran_file = typer.prompt("fpm.toml(fortran) file path", default="fpm.toml")
    if fortran_file is not None or fortran_file != "":
        options["fortran_file"] = fortran_file

    options["no_sync_pom_xml"] = not typer.confirm(
        "Do you want to sync to a pom.xml file?", default=True
    )
    if pom_xml_file := typer.prompt("pom.xml file path", default="pom.xml"):
        options["pom_xml_file"] = pom_xml_file

    options["no_sync_mkdocs"] = not typer.confirm(
        "Do you want to sync to a mkdocs.yml file?", default=True
    )
    if mkdocs_file := typer.prompt("mkdocs.yml file path", default="mkdocs.yml"):
        options["mkdocs_file"] = mkdocs_file

    options["no_sync_rust"] = not typer.confirm(
        "Do you want to sync to a Cargo.toml file?", default=True
    )
    if rust_file := typer.prompt("Cargo.toml file path", default="Cargo.toml"):
        options["rust_file"] = rust_file

    # ----

    options["show_info"] = typer.confirm(
        "Do you want to show info about the sync process?"
    )
    options["verbose"] = typer.confirm("Do you want to show verbose logs?")
    options["debug"] = typer.confirm("Do you want to show debug logs?")

    set_log_level(
        SomesyLogLevel.from_flags(
            debug=options["debug"],
            verbose=options["verbose"],
            info=options["show_info"],
        )
    )

    logger.debug(f"CLI options entered: {options}")

    init_config(input_file, options)
    logger.info(
        f"[bold green]Input file is updated/created at {input_file}[/bold green]"
    )