Skip to content

Commit

Permalink
Merge pull request #2 from seggewiss/1.0.0
Browse files Browse the repository at this point in the history
Implement v1.0.0
  • Loading branch information
seggewiss authored Sep 22, 2023
2 parents 19f90f9 + 9938070 commit 6bb4ccd
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 102 deletions.
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Warning

This program will change all files with the specified extension. This software is best used with a GIT repository.
Make sure you don't have any uncomitted and pushed changes. Then and only then run the software.

# About this project
This tool will refactor component usages in templates according to Vue 3.
If your component uses `model` to define different props as the default model and change events, this is no longer supported with Vue 3.
Expand Down Expand Up @@ -55,20 +60,24 @@ If we combine the event listeners with v-models this would trigger some eslint r
# Usage
It's recommended that you add the released binary for your os to your `$PATH`.

## Arguments

The binary needs at least 3 arguments.
## Flags

1. The component name without the leading carret e.g. `sw-entity-single-select`
2. The model prop name e.g. `value`
3. The previously emitted even name e.g. `change`
4. Optional: Specify the working directory, where the binary searches for `.twig` files
| Flag | Type | Default value | Description |
| ---------- | ------- | ------------------- | ------------------------------------------------------------------------------------- |
| -auto | boolean | false | Automatically fix all base components. Overrides `-component`, `-model` and `-event`! |
| -component | string | "" | The component you want to fix up cross file |
| -model | string | "value" | The model used by the component |
| -event | string | "input" | The event name used to update the model |
| -directory | string | Current working dir | The directory being searched |
| -extension | string | ".twig" | The extension of files being searched for components |
| -help | boolean | false | Displays the above list excluding -help |

If the fourth parameter is left out the current working dir is taken instead.
### Examples

```shell
$ cd <shopwareRoot>/src/Administration/Resources/app/administration
$ v3r sw-component-name-without modelName eventName
$ v3r -component=sw-component-name-without -model=modelName -event=eventName
$ v3r -auto
```

## Check the results
Expand Down
189 changes: 189 additions & 0 deletions internal/components/components.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package components

type Component struct {
Name string
Model string
Event string
}

func GetAutoFixableComponents() []Component {
return []Component{
// src/app/component/form
{
Name: "sw-url-field",
Model: "value",
Event: "input",
},
{
Name: "sw-textarea-field",
Model: "value",
Event: "input",
},
{
Name: "sw-text-field",
Model: "value",
Event: "input",
},
{
Name: "sw-text-editor",
Model: "value",
Event: "input",
},
{
Name: "sw-tagged-field",
Model: "value",
Event: "change",
},
{
Name: "sw-switch-field",
Model: "value",
Event: "change",
},
{
Name: "sw-select-number-field",
Model: "value",
Event: "change",
},
{
Name: "sw-select-field",
Model: "value",
Event: "change",
},
{
Name: "sw-radio-field",
Model: "value",
Event: "change",
},
{
Name: "sw-purchase-price-field",
Model: "price",
Event: "input",
},
{
Name: "sw-price-field",
Model: "price",
Event: "change",
},
{
Name: "sw-password-field",
Model: "value",
Event: "input",
},
{
Name: "sw-number-field",
Model: "value",
Event: "input",
},
{
Name: "sw-gtc-checkbox",
Model: "value",
Event: "change",
},
{
Name: "sw-form-field-renderer",
Model: "value",
Event: "input",
},
{
Name: "sw-file-input",
Model: "value",
Event: "change",
},
{
Name: "sw-email-field",
Model: "value",
Event: "input",
},
{
Name: "sw-dynamic-url-field",
Model: "value",
Event: "input",
},
{
Name: "sw-confirm-field",
Model: "value",
Event: "input",
},
{
Name: "sw-compact-colorpicker",
Model: "value",
Event: "input",
},
{
Name: "sw-colorpicker",
Model: "value",
Event: "input",
},
{
Name: "sw-checkbox-field",
Model: "value",
Event: "change",
},
{
Name: "sw-boolean-radio-group",
Model: "value",
Event: "change",
},
{
Name: "sw-single-select",
Model: "value",
Event: "change",
},
{
Name: "sw-multi-tag-select",
Model: "value",
Event: "change",
},
{
Name: "sw-multi-tag-ip-select",
Model: "value",
Event: "change",
},
{
Name: "sw-multi-select",
Model: "value",
Event: "change",
},
{
Name: "sw-grouped-single-select",
Model: "value",
Event: "change",
},
{
Name: "sw-entity-single-select",
Model: "value",
Event: "change",
},
{
Name: "sw-entity-multi-select",
Model: "entityCollection",
Event: "change",
},
{
Name: "sw-entity-tag-select",
Model: "entityCollection",
Event: "change",
},
{
Name: "sw-entity-multi-id-select",
Model: "ids",
Event: "change",
},
{
Name: "sw-entity-many-to-many-select",
Model: "entityCollection",
Event: "change",
},
// src/app/component/base
{
Name: "sw-button-process",
Model: "processSuccess", // There should be no v-model for this but the event needs to be changed
Event: "process-finish",
},
{
Name: "sw-simple-search-field",
Model: "value",
Event: "input",
},
}
}
40 changes: 40 additions & 0 deletions internal/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package options

import (
"flag"
"os"
)

type Options struct {
Auto bool
WorkingDirectory string
ComponentName string
ModelName string
EventName string
FileExtension string
}

func GetOptions() Options {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}

autoPtr := flag.Bool("auto", false, "Automatically fix all base components. Overrides -component, -model and -event!")
componentNamePtr := flag.String("component", "", "The name of the component to refactor")
modelNamePtr := flag.String("model", "value", "The name of the model to use")
eventNamePtr := flag.String("event", "input", "The name of the event to use")
direcotryPtr := flag.String("directory", cwd, "The directory to search for components")
extensionPtr := flag.String("extension", ".twig", "The file extension to search for")

flag.Parse()

return Options{
Auto: *autoPtr,
WorkingDirectory: *direcotryPtr,
ComponentName: *componentNamePtr,
ModelName: *modelNamePtr,
EventName: *eventNamePtr,
FileExtension: *extensionPtr,
}
}
17 changes: 17 additions & 0 deletions internal/processors/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package processors

import "regexp"

type eventProcessor struct{}

func (p *eventProcessor) Process(options *ProcessorOptions) string {
// https://regex101.com/r/VkXBIA/1
eventRegex := regexp.MustCompile(`(<` + options.ComponentName + `\n( *)[a-zA-Z0-9.\-+*#:_$(')@!=>"{%}\[\]? \n|/` + "`" + `&]*)@` + options.EventName + `="(.*)"`)
eventFixed := eventRegex.ReplaceAllString(options.Text, "${1}{% if VUE3 %}\n${2}@update:"+options.ModelName+"=\"${3}\"\n${2}{% else %}\n${2}@"+options.EventName+"=\"${3}\"\n${2}{% endif %}")

return eventFixed
}

func NewEventProcessor() Processor {
return &eventProcessor{}
}
Loading

0 comments on commit 6bb4ccd

Please sign in to comment.