yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

Gii console command does not overwrite files in non-interactive mode #5613

Closed motin closed 10 years ago

motin commented 10 years ago

This makes it cumbersome to re-generate models and crud in an automated or scripted manner.

A PR to make this possible was available at https://github.com/yiisoft/yii2/pull/5611 but is no longer relevant since the --generate property was removed.

Related discussion found in https://github.com/yiisoft/yii2/issues/5610

Use-case:

For generating a batch of models and CRUDs during very early development. It's very cumbersome to manually use y or ya when there are a lot of changes in many models. But this applies for all Gii actions which may overwrite a large number of files. /@schmunk42

qiangxue commented 10 years ago

Could you help test it? Thanks!

motin commented 10 years ago

Works great over here :)

Help output is correct:

./yii help gii/model
...
--overwrite: boolean, 0 or 1 (defaults to 0)
  whether to overwrite all existing code files when in non-interactive mode.
  Defaults to false, meaning none of the existing code files will be overwritten.
  This option is used only when `--interactive=0`.
...

And the behavior is just as expected:

$ ./yii gii/model --tableName=* --interactive=0
Running 'Model Generator'...

The following files will be generated:
  [unchanged] models/Migration.php

No files were chosen to be generated.

$ echo "a-change" >> models/Migration.php

$ ./yii gii/model --tableName=* --interactive=0
Running 'Model Generator'...

The following files will be generated:
    [changed] models/Migration.php

No files were chosen to be generated.

$ ./yii gii/model --tableName=* --interactive=0
Running 'Model Generator'...

The following files will be generated:
    [changed] models/Migration.php

No files were chosen to be generated.

$ ./yii gii/model --tableName=* --interactive=0 --overwrite=1
Running 'Model Generator'...

The following files will be generated:
    [changed] models/Migration.php

Files were generated successfully!
Generating code using template "/local/path/to/yii2-pr-base/extensions/gii/generators/model/default"...
 overwrote models/Migration.php
done!

$ ./yii gii/crud --interactive=0 --modelClass=\\app\\models\\Migration --controllerClass=\\app\\controllers\\MigrationController
Running 'CRUD Generator'...

The following files will be generated:
  [unchanged] controllers/MigrationController.php
  [unchanged] views/migration/_form.php
  [unchanged] views/migration/create.php
  [unchanged] views/migration/index.php
  [unchanged] views/migration/update.php
  [unchanged] views/migration/view.php

No files were chosen to be generated.

$ echo "a-change" >> controllers/MigrationController.php

$ ./yii gii/crud --interactive=0 --modelClass=\\app\\models\\Migration --controllerClass=\\app\\controllers\\MigrationController
Running 'CRUD Generator'...

The following files will be generated:
    [changed] controllers/MigrationController.php
  [unchanged] views/migration/_form.php
  [unchanged] views/migration/create.php
  [unchanged] views/migration/index.php
  [unchanged] views/migration/update.php
  [unchanged] views/migration/view.php

No files were chosen to be generated.

$ ./yii gii/crud --interactive=0 --overwrite=1 --modelClass=\\app\\models\\Migration --controllerClass=\\app\\controllers\\MigrationController
Running 'CRUD Generator'...

The following files will be generated:
    [changed] controllers/MigrationController.php
  [unchanged] views/migration/_form.php
  [unchanged] views/migration/create.php
  [unchanged] views/migration/index.php
  [unchanged] views/migration/update.php
  [unchanged] views/migration/view.php

Files were generated successfully!
Generating code using template "/local/path/to/yii2-pr-base/extensions/gii/generators/crud/default"...
 overwrote controllers/MigrationController.php
   skipped views/migration/_form.php
   skipped views/migration/create.php
   skipped views/migration/index.php
   skipped views/migration/update.php
   skipped views/migration/view.php
done!

$ ./yii gii/crud --interactive=1 --overwrite=1 --modelClass=\\app\\models\\Migration --controllerClass=\\app\\controllers\\MigrationController
Running 'CRUD Generator'...

The following files will be generated:
  [unchanged] controllers/MigrationController.php
  [unchanged] views/migration/_form.php
  [unchanged] views/migration/create.php
  [unchanged] views/migration/index.php
  [unchanged] views/migration/update.php
  [unchanged] views/migration/view.php

No files were chosen to be generated.

$ echo "a-change" >> controllers/MigrationController.php

$ ./yii gii/crud --interactive=1 --overwrite=1 --modelClass=\\app\\models\\Migration --controllerClass=\\app\\controllers\\MigrationController
Running 'CRUD Generator'...

The following files will be generated:
    [changed] controllers/MigrationController.php
Do you want to overwrite this file? [y,n,ya,na,?]:
schmunk42 commented 10 years ago

Works very nice! Thanks @qiangxue.

One thing to mention (again, sorry) ... shorter release cycles, this would be a very useful fix, which is a typical patch-level update. But I saw over 100 issue open atm. for 2.0.1, which means it will take quite some time to have the feature available with a stable tag. Would be nice, if you could consider releasing Yii 2 more often.

qiangxue commented 10 years ago

See it here: https://github.com/yiisoft/yii2/blob/master/docs/internals/versions.md