App-Sqitch

 view release on metacpan or  search on metacpan

lib/App/Sqitch/Command/init.pm  view on Meta::CPAN

    if ( my $uri = $opt->{uri} ) {
        require URI;
        $opt->{uri} = 'URI'->new($uri);
    }

    return $opt;
}

sub write_config {
    my $self    = shift;
    my $sqitch  = $self->sqitch;
    my $config  = $sqitch->config;
    my $file    = $config->local_file;
    if ( -f $file ) {

        # Do nothing? Update config?
        return $self;
    }

    my ( @vars, @comments );

    # Get the props, and make sure the target can find the engine.
    my $props  = $self->properties;
    my $target = $self->config_target;

    # Write the engine from --engine or core.engine.
    my $ekey   = $props->{engine} || $target->engine_key;
    if ($ekey) {
        push @vars => {
            key   => "core.engine",
            value => $ekey,
        };
    }
    else {
        push @comments => "\tengine = ";
    }

    # Add core properties.
    for my $name (qw(
        plan_file
        top_dir
    )) {
        # Set properties passed on the command-line.
        if ( my $val = $props->{$name} ) {
            push @vars => {
                key   => "core.$name",
                value => $val,
            };
        }
        else {
            my $val //= $target->$name // '';
            push @comments => "\t$name = $val";
        }
    }

    # Add script options passed to the init command. No comments if not set.
    for my $attr (qw(
        extension
        deploy_dir
        revert_dir
        verify_dir
        reworked_dir
        reworked_deploy_dir
        reworked_revert_dir
        reworked_verify_dir
    )) {
        push @vars => { key => "core.$attr", value => $props->{$attr} }
            if defined $props->{$attr};
    }

    # Add variables.
    if (my $vars = $props->{variables}) {
        push @vars => map {{
            key   => "core.variables.$_",
            value => $vars->{$_},
        }} keys %{ $vars };
    }

    # Emit them.
    if (@vars) {
        $config->group_set( $file => \@vars );
    }
    else {
        unshift @comments => '[core]';
    }

    # Emit the comments.
    $config->add_comment(
        filename => $file,
        indented => 1,
        comment  => join "\n" => @comments,
    ) if @comments;

    if ($ekey) {
        # Write out the engine.$engine section.
        my $config_key  = "engine.$ekey";
        @comments = @vars = ();

        for my $key (qw(target registry client)) {
            # Was it passed as an option?
            if ( my $val = $props->{$key} ) {
                push @vars => {
                    key   => "$config_key.$key",
                    value => $val,
                };
                # We're good on this one.
                next;
            }

            # No value, but add it as a comment, possibly with a default.
            my $def = $target->$key
                // $config->get( key => "$config_key.$key" )
                // '';
            push @comments => "\t$key = $def";
        }

        if (@vars) {
            # Emit them.
            $config->group_set( $file => \@vars ) if @vars;
        }
        else {
            # Still want the section, emit it as a comment.
            unshift @comments => qq{[engine "$ekey"]};
        }



( run in 0.882 second using v1.01-cache-2.11-cpan-5a3173703d6 )