vitonsky / charmix

Tool to generate project files structure from archetypes (file structure template created by users)
https://www.npmjs.com/package/charmix
Apache License 2.0
1 stars 0 forks source link

Implement merge behavior #28

Open vitonsky opened 2 years ago

vitonsky commented 2 years ago

It's frequently situation when you update config for your linter and want to update this config for all your projects. Right now you have to copy all changed lines.

It would be useful if you could apply archetype and its will update your configs automatically.

To do it, we may implement merge behavior. User will use archetype as usual, but exists files will merge with archetype files.

However, users not wanna to merge all files and what about case when user did removed an config property, but archetype contains this property?

We have to implement some mechanics to solve this merge problems.

It may be config file where user specify fully managed files, files that must be never touch and lines in files or paths in structures, that may be managed or must be never changed.

Or we may analyze git log to inference this options automatically

vitonsky commented 1 year ago

It is better approach to analyze git log to find changes in files and consider this changes for update.

We should to transform each file to AST, find changes for each commit related to a archetype files and apply new archetype with remove nodes that been removed and add new nodes if it does not exists.

Also, we have to mark commits where archetypes been applied. We may be format like

[{
  "commit": "repository sha hash",
  "archetype": "unique archetype id",
  "version": "archetype version"
}]
vitonsky commented 1 year ago

Changes example:

Update v1

User file: empty

Archetype v1:

{
  "foo": 1,
  "bar": 2,
}

Result:

{
  "foo": 1,
  "bar": 2,
}

Update v2

User file:

{
  "foo": 1,
  "bar": 2,
}

Archetype v2:

{
  "foo": 1,
  "bar": 2,
  "baz": 3,
}

Result:

{
  "foo": 1,
  "bar": 2,
  "baz": 3,
}

Update v3

User file:

{
  "foo": 1,
  "bar": 2,
  "baz": 3,
}

Archetype v3:

{
  "foo": 4,
  "baz": 3,
}

Result

{
  "foo": 4,
  "baz": 3,
}
vitonsky commented 1 year ago

Example with solve conflict with user changes:

Update v1

User file: empty

Archetype v1:

{
  "foo": 1,
  "bar": 2,
}

Result:

{
  "foo": 1,
  "bar": 2,
}

All changes applied for empty file

Update v2

User file:

{
  "bar": 2,
  "qux": 777,
}

Archetype v2:

{
  "foo": 1,
  "bar": 2,
  "baz": 3,
}

Result:

{
  "bar": 2,
  "qux": 777,
  "baz": 3,
}

Property foo does not added, because:

Update v3

User file:

{
  "bar": 2,
  "qux": 777,
  "baz": 3,
}

Archetype v3:

{
  "foo": 4,
  "baz": 3,
}

Result

{
  "qux": 777,
  "baz": 3,
}

Property foo does not added, by reasons from update v2.

Property bar been removed, because:

If value of property bar would been changed, it would be not managed and will not removed if not specified flag force removed props are managed