Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

migrate a specific dir or file

Louis Choquel3c551d560587
diff --git a/pipelex/cli/commands/migrate_cmd.py b/pipelex/cli/commands/migrate_cmd.py
index bcb8b442c..4d191bba4 100644
--- a/pipelex/cli/commands/migrate_cmd.py
+++ b/pipelex/cli/commands/migrate_cmd.py
@@ -11,25 +11,31 @@ from .common import is_pipelex_libraries_folder
def do_migrate(
- relative_config_folder_path: str = "./pipelex_libraries",
+ target_path: str = "./pipelex_libraries",
dry_run: bool = False,
backups: bool = True,
) -> None:
- """Migrate TOML files to new syntax (Concept = -> definition = and PipeClassName = -> type/definition)."""
- config_path = Path(relative_config_folder_path)
+ """Migrate TOML files to new syntax (Concept = -> definition = and PipeClassName = -> type/definition).
- if is_pipelex_libraries_folder(relative_config_folder_path):
- pipelines_dir = config_path / "pipelines"
+ The path can be:
+ - A Pipelex libraries folder (we'll use its `pipelines` subfolder)
+ - A directory containing TOML files
+ - A single TOML file
+ """
+ config_path = Path(target_path)
+
+ if is_pipelex_libraries_folder(target_path):
+ pipelines_dir_or_file = config_path / "pipelines"
else:
- pipelines_dir = config_path
+ pipelines_dir_or_file = config_path
- if not pipelines_dir.exists():
- typer.echo(f"❌ Directory not found at '{pipelines_dir}'")
+ if not pipelines_dir_or_file.exists():
+ typer.echo(f"❌ Path not found at '{pipelines_dir_or_file}'")
raise typer.Exit(1)
try:
result = migrate_concept_syntax(
- directory=pipelines_dir,
+ directory=pipelines_dir_or_file,
create_backups=backups and not dry_run,
dry_run=dry_run,
)
@@ -49,13 +55,19 @@ def do_migrate(
if dry_run:
migrator = TomlMigrator()
+ # For display, if a single file path was provided, show relative to its parent
+ base_for_print = pipelines_dir_or_file if pipelines_dir_or_file.is_dir() else pipelines_dir_or_file.parent
for file_path in result.modified_files:
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
changes = migrator.get_migration_preview(content)
- typer.echo(f"\n📄 {file_path.relative_to(pipelines_dir)}:")
+ try:
+ rel = file_path.relative_to(base_for_print)
+ except Exception:
+ rel = Path(file_path.name)
+ typer.echo(f"\n📄 {rel}:")
for change in changes:
typer.echo(f" Line {change['line_number']}: {change['old_line']} → {change['new_line']}")
except Exception as exc:
@@ -65,8 +77,13 @@ def do_migrate(
typer.echo(" Run without --dry-run to apply these changes")
else:
create_backups = backups and not dry_run
+ base_for_print = pipelines_dir_or_file if pipelines_dir_or_file.is_dir() else pipelines_dir_or_file.parent
for file_path in result.modified_files:
- typer.echo(f"✅ Migrated {file_path.relative_to(pipelines_dir)}")
+ try:
+ rel = file_path.relative_to(base_for_print)
+ except Exception:
+ rel = Path(file_path.name)
+ typer.echo(f"✅ Migrated {rel}")
if create_backups:
backup_path = file_path.with_suffix(".toml.backup")
typer.echo(f" Backup saved to {backup_path.name}")
@@ -89,10 +106,15 @@ migrate_app = typer.Typer(help="Migration commands")
@migrate_app.command("run")
def migrate_cmd(
- relative_config_folder_path: Annotated[
- str, typer.Option("--config-folder-path", "-c", help="Relative path to the config folder path")
+ target_path: Annotated[
+ str,
+ typer.Option(
+ "--path",
+ "-p",
+ help=("Path to Pipelex libraries folder (uses its pipelines), a directory of TOML files, or a single TOML file"),
+ ),
] = "./pipelex_libraries",
dry_run: Annotated[bool, typer.Option("--dry-run", help="Preview changes without applying them")] = False,
backups: Annotated[bool, typer.Option("--backups/--no-backups", help="Create backup files before migration")] = True,
) -> None:
- do_migrate(relative_config_folder_path=relative_config_folder_path, dry_run=dry_run, backups=backups)
+ do_migrate(target_path=target_path, dry_run=dry_run, backups=backups)
diff --git a/pipelex/tools/migrate/migrate_v0_1_0_to_v0_2_0.py b/pipelex/tools/migrate/migrate_v0_1_0_to_v0_2_0.py
index adf889cf8..dc6ab0318 100644
--- a/pipelex/tools/migrate/migrate_v0_1_0_to_v0_2_0.py
+++ b/pipelex/tools/migrate/migrate_v0_1_0_to_v0_2_0.py
@@ -247,13 +247,68 @@ def migrate_concept_syntax(directory: Path, create_backups: bool = True, dry_run
"""
Convenience function to migrate TOML files from Concept = to definition = syntax.
+ This function accepts either a directory (the historical behavior) or a path to a single TOML file.
+
Args:
- directory: Directory containing TOML files to migrate
- create_backups: Whether to create backup files before migration
+ directory: Path to a directory containing TOML files to migrate, or a single TOML file
+ create_backups: Whether to create backup files before migration (ignored in dry-run)
dry_run: If True, only preview changes without applying them
Returns:
MigrationResult with migration statistics
"""
migrator = TomlMigrator()
- return migrator.migrate_directory(directory, create_backups, dry_run)
+
+ # Support passing a single TOML file path in addition to a directory
+ input_path = directory
+ if input_path.is_file():
+ result = MigrationResult()
+ result.files_processed = 1
+
+ if input_path.suffix.lower() != ".toml":
+ result.errors.append(f"Provided file is not a .toml file: {input_path}")
+ return result
+
+ try:
+ with open(input_path, "r", encoding="utf-8") as f:
+ content = f.read()
+ except Exception as e:
+ result.errors.append(f"Failed to read file {input_path}: {e}")
+ return result
+
+ if not migrator.needs_migration(content):
+ return result
+
+ # Count only concept changes for consistency with directory migration statistics
+ def _is_inside_multiline_string(text: str, position: int) -> bool:
+ """Local helper to check if a position is inside a TOML multiline string."""
+ text_before = text[:position]
+ triple_double_quotes = text_before.count('"""')
+ triple_single_quotes = text_before.count("'''")
+ return (triple_double_quotes % 2 == 1) or (triple_single_quotes % 2 == 1)
+
+ matches = list(migrator.concept_pattern.finditer(content))
+ changes_count = 0
+ for match in matches:
+ if not _is_inside_multiline_string(content, match.start()):
+ changes_count += 1
+
+ if dry_run:
+ result.files_modified = 1
+ result.total_changes = changes_count
+ result.modified_files.append(input_path)
+ return result
+
+ # Perform actual migration
+ try:
+ actual_changes = migrator.migrate_file(input_path, create_backup=create_backups)
+ result.files_modified = 1
+ result.total_changes = actual_changes
+ result.modified_files.append(input_path)
+ except Exception as e:
+ result.errors.append(f"Error processing {input_path}: {e}")
+
+ return result
+
+ # Default: treat as directory (existing behavior)
+ return migrator.migrate_directory(input_path, create_backups, dry_run)