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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
@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:
        ignored = [
            name
            for name, given in (
                ("--output-file", output_file != Path("somesy.toml")),
                ("--overwrite", overwrite),
                ("--non-interactive", non_interactive),
            )
            if given
        ]
        if ignored:
            logger.warning(
                f"Option(s) {', '.join(ignored)} of 'init' are ignored by "
                f"'init {ctx.invoked_subcommand}'."
            )
        return
    root = Path.cwd()
    output = output_file if output_file.is_absolute() else root / output_file
    if output.exists() and not overwrite:
        raise FileExistsError(f"Output file already exists: {output}")
    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)
    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
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
@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
    try:
        input_file_default = discover_input()
    except FileNotFoundError:
        input_file_default = Path("somesy.toml")  # first-time creation

    # prompt for inputs
    input_file = Path(typer.prompt("Input file path", default=input_file_default))
    # validate the target before asking anything else
    if not input_file.exists() and not SomesyInput.is_somesy_file_path(input_file):
        raise FileNotFoundError(
            f"Input file not found: {input_file} (only somesy.toml files can be "
            "created by this command)."
        )
    options: dict[str, Any] = {"input_file": Path(input_file)}

    for key, label, default_file, default_sync in _CONFIG_TARGETS:
        options[f"no_sync_{key}"] = not typer.confirm(
            f"Do you want to sync to a {label} file?", default=default_sync
        )
        if target_file := typer.prompt(f"{label} file path", default=default_file):
            options[f"{key}_file"] = target_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]"
    )