1616 * merges chains of rules into a single pass, and applies the merged rules
1717 * directly
1818 * on raw YAML text lines to preserve comments, formatting, and layout.
19- * <p>
20- * Migration files contain sections (e.g. {@code scripts}, {@code configs})
21- * each with their own list of rules. When migrating, a specific section
22- * is selected so only the relevant rules are applied.
2319 */
2420public final class YamlMigrator {
2521
26- /** Well-known section name for script migrations. */
27- public static final String SECTION_SCRIPTS = "scripts" ;
28-
29- /** Well-known section name for config migrations. */
30- public static final String SECTION_CONFIGS = "configs" ;
31-
3222 private static final Pattern VERSION_LINE = Pattern .compile ("(version:\\ s*)\\ d+" );
3323 private static final Pattern WILDCARD_PATTERN = Pattern .compile ("^(.+?)\\ [\\ *]$" );
3424
@@ -63,53 +53,29 @@ public int detectVersion(String yaml) {
6353 return -1 ;
6454 }
6555
66- /**
67- * @param yaml the raw YAML content
68- * @param section the migration section to use
69- * @return the result containing the migrated YAML or an error
70- */
71- public MigrationResult migrate (String yaml , String section ) {
72- return migrate (yaml , section , -1 );
73- }
74-
75- /**
76- * Migrates a raw YAML string from its detected version to the current
77- * version, using rules from the given section.
78- *
79- * @param yaml the raw YAML content
80- * @param section the migration section to use (e.g. "scripts" or
81- * "configs")
82- * @param defaultVersion the version to assume if no {@code version:} field is
83- * found,
84- * or {@code -1} to require an explicit version field
85- * @return the result containing the migrated YAML or an error
86- */
87- public MigrationResult migrate (String yaml , String section , int defaultVersion ) {
56+ public MigrationResult migrate (String yaml ) {
8857 int from = detectVersion (yaml );
8958 if (from < 0 ) {
90- if (defaultVersion > 0 ) {
91- from = defaultVersion ;
92- } else {
93- return MigrationResult .error ("Could not detect version" );
94- }
59+ return MigrationResult .error ("Could not detect script version" );
9560 }
9661 if (from == currentVersion ) {
9762 return MigrationResult .skip (from );
9863 }
9964 if (from > currentVersion ) {
10065 return MigrationResult .error (String .format (
101- "Version %d is newer than the current version %d" , from , currentVersion ));
66+ "Script version %d is newer than the current version %d" , from ,
67+ currentVersion ));
10268 }
103- return migrate (yaml , section , from , currentVersion );
69+ return migrate (yaml , from , currentVersion );
10470 }
10571
10672 /**
10773 * Migrates a raw YAML string from {@code from} to {@code to} by applying
108- * merged rules for the given section directly on text lines, preserving
109- * all comments, blank lines, and formatting.
74+ * merged rules directly on text lines, preserving all comments, blank
75+ * lines, and formatting.
11076 */
111- public MigrationResult migrate (String yaml , String section , int from , int to ) {
112- List <MigrationRule > merged = mergeRules (from , to , section );
77+ public MigrationResult migrate (String yaml , int from , int to ) {
78+ List <MigrationRule > merged = mergeRules (from , to );
11379 List <String > lines = new ArrayList <>(Arrays .asList (yaml .split ("\n " , -1 )));
11480
11581 List <TargetAction > modifyActions = new ArrayList <>();
@@ -461,7 +427,7 @@ private record TargetAction(MigrationRule rule, int start, int end) {
461427 private record InsertAction (int lineIndex , String content ) {
462428 }
463429
464- List <MigrationRule > mergeRules (int from , int to , String section ) {
430+ List <MigrationRule > mergeRules (int from , int to ) {
465431 Map <String , String > pathMap = new LinkedHashMap <>();
466432 Map <String , MigrationRule > addedRules = new LinkedHashMap <>();
467433 List <MigrationRule > effective = new ArrayList <>();
@@ -472,7 +438,7 @@ List<MigrationRule> mergeRules(int from, int to, String section) {
472438 continue ;
473439 }
474440
475- for (MigrationRule rule : step .rules (section )) {
441+ for (MigrationRule rule : step .rules ()) {
476442 switch (rule .type ()) {
477443 case REMOVE -> {
478444 String resolvedPath = resolveCurrentPath (pathMap , rule .path ());
@@ -579,32 +545,16 @@ private void loadSteps() {
579545 continue ;
580546 }
581547
582- Map <String , List <MigrationRule >> sections = new LinkedHashMap <>();
583-
584- for (var entry : map .entrySet ()) {
585- String key = Objects .toString (entry .getKey (), "" );
586- Object value = entry .getValue ();
587-
588- if (!(value instanceof List <?> ruleList )) {
589- continue ;
590- }
591-
592- List <MigrationRule > rules = parseRules (ruleList , filename );
593- if (!rules .isEmpty ()) {
594- sections .put (key , List .copyOf (rules ));
595- }
548+ Object rulesObj = map .get ("rules" );
549+ List <MigrationRule > rules = new ArrayList <>();
550+ if (rulesObj instanceof List <?> ruleList ) {
551+ rules .addAll (parseRules (ruleList , filename ));
596552 }
597553
598- if (sections .isEmpty ()) {
599- continue ;
600- }
601-
602- MigrationStep step = new MigrationStep (from , from + 1 , sections );
554+ MigrationStep step = new MigrationStep (from , from + 1 , rules );
603555 steps .put (from , step );
604-
605- int totalRules = sections .values ().stream ().mapToInt (List ::size ).sum ();
606- Log .debug ("Loaded migration step {} -> {} with {} rule(s) across {} section(s)" ,
607- from , from + 1 , totalRules , sections .size ());
556+ Log .debug ("Loaded migration step {} -> {} with {} rule(s)" ,
557+ from , from + 1 , rules .size ());
608558
609559 } catch (Exception e ) {
610560 Log .warn ("Failed to load migration file '{}': {}" , filename , e .getMessage ());
0 commit comments