App-Acmeman

 view release on metacpan or  search on metacpan

lib/App/Acmeman/Apache/Layout/slackware.pm  view on Meta::CPAN

	    if ($node->name =~ /^loadmodule$/i) {
		$last_loadmodule = $node->locus;
	    }
	}
	
	if ($need_macro_module) {
	    if ($node->type eq 'comment') {
		if ($node->value =~ m{^\s*(loadmodule\s+macro_module\s.*)}i) {
		    debug(3, "Will uncomment ".$node->locus. " ".$node->value);
		    $self->add_command($node->locus, \&_replace_directive_at, $1);
		    $last_loadmodule = $node->locus;
		    $need_macro_module = 0;
		}
	    }
	}
	
	if (!$letsencrypt_macro_loaded) {
	    if ($node->type eq 'directive'
		&& $node->name =~ /^include/i
		&& $node->value =~ m{^.+/httpd-vhosts.conf\s*$}) {
		my $locus = $node->locus->has_file($master_config_file)
		    ? $node->locus : $last_master_locus;
		debug(3, "Will insert \"$include_text\" before $locus");
		$self->add_command($locus, \&_insert_directive_before,
				   $include_text);
		$letsencrypt_macro_loaded = 3;
	    }
	}
    }

    if ($need_macro_module && $last_loadmodule) {
	debug(3, "Will insert a LoadModule directive after ".$last_loadmodule);
	$self->add_command($last_loadmodule,
			   \&_insert_directive_after,
	           q{LoadModule macro_module lib64/httpd/modules/mod_macro.so});
	$need_macro_module = 0;
	
	$last_loadmodule->fixup_lines($app->filename => 1);
    }

    if (!$letsencrypt_macro_loaded && !$need_macro_module) {
	if ($stop_search) {
	    debug(3, "Will insert \"$include_text\" before $stop_search");
	    $self->add_command($stop_search, \&_insert_directive_before,
			       $include_text);
	} else {
	    debug(3, "Will insert \"$include_text\" after $last_loadmodule");
	    $self->add_command($last_loadmodule, \&_insert_directive_after,
			       $include_text);
        }	    
    }

    $self->run_commands;
}

sub add_command {
    my ($self, $locus, $command, $text) = @_;
    my $file = ($locus->filenames)[0];
    my $line = ($locus->filelines($file))[0];

    push @{$self->{_edits}{$file}}, { line => $line, command => $command, text => $text };
}

sub run_commands {
    my ($self) = @_;
    foreach my $file (keys %{$self->{_edits}}) {
	$self->run_commands_for_file($file);
    }
    $self->apache_modules(undef);
}

sub run_commands_for_file {
    my ($self, $file) = @_;
    my @commands = sort { $a->{line} <=> $b->{line} } @{$self->{_edits}{$file}};

    my $app = new Apache::Config::Preproc(
        $file,
	'-no-comment-grouping',
        -expand => [ 'locus' ])
	or do {
	    error("can't parse apache configuration file $file: $Apache::Admin::Config::ERROR");
	    return;
    };

    foreach my $node ($app->select) {
	last if (!@commands);
	my $line = ($node->locus->filelines(($node->locus->filenames)[0]))[0];
	if ($commands[0]->{line} == $line) {
	    my $cmd = shift @commands;
	    $self->${\ $cmd->{command} }($app, $node, $cmd->{text});
	}
    }

    if (@commands) {
	error((0+@commands) . " left unmatched in edit queue for file $file");
	error("$file left unchanged");
    } else {
	my $backup_name = backup_copy($app->filename, error => \my $error);
	if ($backup_name) {
	    debug(1, "modifying ".$app->filename."; prior version saved in $backup_name");
	} elsif ($error) {
	    error("can't backup ".$app->filename.": $error");
	    error("file left unchanged");
	    return;
	}
	$app->save;
    }
}

sub _replace_directive_at {
    my ($self, $app, $node, $text) = @_;
    $self->_insert_directive_after($app, $node, $text);
    $node->delete;
}

sub _insert_directive_after {
    my ($self, $app, $node, $text) = @_;
    my ($name, $value) = split /\s+/, $text, 2;
    $app->add('directive', $name, $value, -after => $node);
}

sub _insert_directive_before {
    my ($self, $app, $node, $text) = @_;
    my ($name, $value) = split /\s+/, $text, 2;
    $app->add('directive', $name, $value, -before => $node);
}


1;

	    



( run in 1.797 second using v1.01-cache-2.11-cpan-13bb782fe5a )