Skip to content

Commit ce4d44e

Browse files
committed
Skip regenerating LICENSE if --license not given
1 parent 66fdb47 commit ce4d44e

1 file changed

Lines changed: 31 additions & 26 deletions

File tree

src/ScaffoldPackageCommand.php

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ public function package_readme( $args, $assoc_args ) {
349349
$package_root = dirname( __DIR__ );
350350
$template_path = $package_root . '/templates/';
351351

352-
$license_input = Utils\get_flag_value( $assoc_args, 'license' );
353-
if ( null === $license_input ) {
352+
$license_input = Utils\get_flag_value( $assoc_args, 'license' );
353+
$license_provided = null !== $license_input;
354+
if ( ! $license_provided ) {
354355
$license_input = isset( $composer_obj['license'] ) ? $composer_obj['license'] : 'MIT';
355356
}
356357
$composer_license = 'none' === strtolower( $license_input ) ? 'proprietary' : $license_input;
@@ -603,32 +604,36 @@ public function package_readme( $args, $assoc_args ) {
603604
WP_CLI::success( 'Created package readme.' );
604605
}
605606

606-
// Only write to composer.json if the license has changed. This protects package_readme from
607-
// overwriting files changed by package. Only when --license changes do we need to write.
608-
$existing_license = isset( $composer_obj['license'] ) ? $composer_obj['license'] : '';
609-
$composer_obj['license'] = $composer_license;
610-
if ( $composer_license !== $existing_license ) {
611-
$composer_json_content = json_encode( $composer_obj, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) . "\n";
612-
if ( false === file_put_contents( $package_dir . '/composer.json', $composer_json_content ) ) {
613-
WP_CLI::error( "Error creating file: {$package_dir}/composer.json" );
607+
// Only update composer.json license and generate the LICENSE file when --license was
608+
// explicitly passed. Without it, leave both untouched.
609+
if ( $license_provided ) {
610+
// Only write to composer.json if the license has changed. This protects package_readme from
611+
// overwriting files changed by package. Only when --license changes do we need to write.
612+
$existing_license = isset( $composer_obj['license'] ) ? $composer_obj['license'] : '';
613+
$composer_obj['license'] = $composer_license;
614+
if ( $composer_license !== $existing_license ) {
615+
$composer_json_content = json_encode( $composer_obj, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) . "\n";
616+
if ( false === file_put_contents( $package_dir . '/composer.json', $composer_json_content ) ) {
617+
WP_CLI::error( "Error creating file: {$package_dir}/composer.json" );
618+
}
614619
}
615-
}
616620

617-
if ( 'none' === strtolower( $license_input ) ) {
618-
if ( is_file( $package_dir . '/LICENSE' ) ) {
619-
unlink( $package_dir . '/LICENSE' );
620-
}
621-
} else {
622-
$license_file_path = $this->resolve_license_file_path( $license_input, $template_path );
623-
if ( null !== $license_file_path ) {
624-
$license_data = [
625-
'year' => gmdate( 'Y' ),
626-
'name' => $composer_obj['name'],
627-
];
628-
$this->create_files(
629-
[ "{$package_dir}/LICENSE" => Utils\mustache_render( $license_file_path, $license_data ) ],
630-
$force
631-
);
621+
if ( 'none' === strtolower( $license_input ) ) {
622+
if ( is_file( $package_dir . '/LICENSE' ) ) {
623+
unlink( $package_dir . '/LICENSE' );
624+
}
625+
} else {
626+
$license_file_path = $this->resolve_license_file_path( $license_input, $template_path );
627+
if ( null !== $license_file_path ) {
628+
$license_data = [
629+
'year' => gmdate( 'Y' ),
630+
'name' => $composer_obj['name'],
631+
];
632+
$this->create_files(
633+
[ "{$package_dir}/LICENSE" => Utils\mustache_render( $license_file_path, $license_data ) ],
634+
$force
635+
);
636+
}
632637
}
633638
}
634639
}

0 commit comments

Comments
 (0)