{"id":17226779,"url":"https://github.com/dantleech/code-mover","last_synced_at":"2025-04-14T01:13:00.238Z","repository":{"id":66290676,"uuid":"14210983","full_name":"dantleech/code-mover","owner":"dantleech","description":"Library for code migration/refactoring automization","archived":false,"fork":false,"pushed_at":"2015-12-08T20:09:16.000Z","size":104,"stargazers_count":21,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-14T01:12:49.759Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dantleech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-07T17:44:47.000Z","updated_at":"2025-01-04T20:10:47.000Z","dependencies_parsed_at":"2023-03-20T21:18:33.875Z","dependency_job_id":null,"html_url":"https://github.com/dantleech/code-mover","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dantleech%2Fcode-mover","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dantleech%2Fcode-mover/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dantleech%2Fcode-mover/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dantleech%2Fcode-mover/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dantleech","download_url":"https://codeload.github.com/dantleech/code-mover/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248804825,"owners_count":21164135,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T04:17:13.815Z","updated_at":"2025-04-14T01:13:00.216Z","avatar_url":"https://github.com/dantleech.png","language":"PHP","funding_links":[],"categories":["杂项","Miscellaneous"],"sub_categories":[],"readme":"# Code Mover\n\n**NOTE**: Do not use this library, it is not maintained and it is wrong.\n\n[![Build Status](https://travis-ci.org/dantleech/code-mover.png?branch=master)](https://travis-ci.org/dantleech/code-mover)\n\nSmall library for migrating code, its sort of analagous to database migrations\nbut instead of applying changes to a database, you apply it to code.\n\n*Note this is a proof of concept* at the moment and has much work to be done.\n\nThis allows you to write and refine code migrations for massive API changes and\nrefactorings.\n\n````php\n// app/CodeMoverMigrations/FormFieldMigrator.php\n// app/CodeMoverMigrations/FormFieldMigrator.php\n\nuse DTL\\CodeMover\\AbstractMigrator;\nuse DTL\\CodeMover\\MoverFile;\n\nclass FormFieldMigrator extends AbstractMigrator\n{\n    protected $fieldTypeMap = array(\n    );\n\n    public function getName()\n    {\n        return 'form_fields';\n    }\n\n    public function getDependencies()\n    {\n        return array('namespaces');\n    }\n\n    public function accepts(AbstractFile $file)\n    {\n        return $file-\u003enameMatches('\\/[a-zA-Z0-9]+Form\\.php*');\n    }\n\n    public function migrate(MigratorContext $context)\n    {\n        $file = $this-\u003econtext-\u003egetFile();\n\n        $file-\u003efindLine('.*foobar.*')-\u003ereplace('foobar', 'barfoo');\n\n        $file-\u003efindLines(array(\n            'use .*?Field;',\n            'use .*Group;'\n        ))-\u003edelete();\n\n        $file-\u003efindLines('$this-\u003eadd\\(new .*?Field')\n            });;\n        $file-\u003efindLines('$this-\u003eadd\\(new .*?Field')\n            -\u003ereplace('\\$this-\u003eadd\\(new (.*?)Field\\(\\'(.*?)\\'', function ($matches) {\n                $fieldType = strtolower($matches[1]);\n                return '$this-\u003eadd('.$matches[2].', '.$fieldType.'\\'';\n            })-\u003eeach(function ($line) {\n                    $line-\u003ematch('(.*)foo(.*)')-\u003eapply(function($line, $match1, $match2) {\n                        // the apply closure is passed the matches from the regex\n                    });\n                });;\n    }\n}\n````\n\nThe above migrator will:\n\n- Replace `foobar` with `barfoo` on lines matching regex `.*foobar.*`\n- Only process files matching the regex pattern `*Form.php`;\n- Delete all lines that match either `use .*Field;` or `use .*Group`\n- Will replace lines like `$this-\u003eadd(new TextAreaField('field_name')` with `$this-\u003eadd('field_name', 'textarea');`\n  - Then apply a closure to each modified line\n\nYou can run it on some code:\n\n````bash\nphp bin/codemover.php migrate ~/myproject/app/CodeMoverMigrations \\\n    --path ~/myproject/src/Bundle1 \\\n    --path ~/myproject/src/Bundle2 \\\n    --name \"*CreateForm.php\"\n````\n\nThis will:\n\n- Run all the migration classes in `~/myproject/app/CodeMoverMigrations`\n- On the code contained in `~/myproject/src/Bundle1` and `~/myproject/src/Bundle2`\n- Only on filenames matching `*CreateForm.php`\n\nAnd generate some output like:\n\n````bash\nAdding migrator: FormFieldMigrator\nAdding migrator: NamespacesMigrator\nResolved migrator order: namespaces, form_fields\nMigrator \"namespaces\" accepts file \"/home/daniel/www/yProximite/yProx/src/Ylly/CmsBundle/Form/Admin/SiteCreateForm.php\"\n  -namespace Ylly\\CmsBundle\\Form\\Admin;\n  +namespace Ylly\\CmsBundle\\Form\\Type\\Admin;\nMigrator \"form_fields\" accepts file \"/home/daniel/www/yProximite/yProx/src/Ylly/CmsBundle/Form/Admin/SiteCreateForm.php\"\n  -use Ylly\\OldFormBundle\\Form\\TextField;\n  -use Ylly\\OldFormBundle\\Form\\ChoiceField;\n  -use Ylly\\OldFormBundle\\Form\\CheckboxField;\n  -use Ylly\\OldFormBundle\\Form\\FieldGroup;\n  -use Ylly\\CmsBundle\\Inheritance\\Form\\InheritanceField;\n  -            $this-\u003eadd(new ChoiceField('company', array(\n  +            $this-\u003eadd(company, choice', array(\n  -            $this-\u003eadd(new TextField('bundleName'));\n  +            $this-\u003eadd(bundleName, text'));\n  -        $this-\u003eadd(new TextField('title'));\n  -        $this-\u003eadd(new TextField('host'));\n  -        $this-\u003eadd(new TextField('localesAsCsv'));\n  -        $this-\u003eadd(new TextField('defaultLocale'));\n  +        $this-\u003eadd(title, text'));\n  +        $this-\u003eadd(host, text'));\n  +        $this-\u003eadd(localesAsCsv, text'));\n  +        $this-\u003eadd(defaultLocale, text'));\n  -        $this-\u003eadd(new ChoiceField('statisticsEngine', array('choices' =\u003e $statisticsEngines)));\n  +        $this-\u003eadd(statisticsEngine, choice', array('choices' =\u003e $statisticsEngines)));\n  -        $this-\u003eadd(new CheckboxField('enableExternalLinks'));\n  -        $this-\u003eadd(new CheckboxField('enableDir'));\n  +        $this-\u003eadd(enableExternalLinks, checkbox'));\n  +        $this-\u003eadd(enableDir, checkbox'));\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdantleech%2Fcode-mover","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdantleech%2Fcode-mover","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdantleech%2Fcode-mover/lists"}