<commit_before>/*
 * Copyright © 2017 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { EditProject } from '@atomist/rug/operations/ProjectEditor';
import { Project } from '@atomist/rug/model/Project';
import { File } from '@atomist/rug/model/File';
import { Pattern } from '@atomist/rug/operations/RugOperation';
import { Editor, Parameter, Tags } from '@atomist/rug/operations/Decorators';
import { PathExpression, PathExpressionEngine } from '@atomist/rug/tree/PathExpression';

import { IsRugArchive } from './RugEditorsPredicates';
import { RugParameters } from './RugParameters';

@Editor("AddManifestYml", "adds a Rug archive manifest")
@Tags("rug", "atomist")
export class AddManifestYml implements EditProject {

    @Parameter({
        displayName: "Rug Archive Name",
        description: "name of your new Rug Archive, typically the same as the repo name",
        pattern: Pattern.project_name,
        validInput: "a valid GitHub repo name containing only alphanumeric, ., -, and _ characters",
        minLength: 1,
        maxLength: 100
    })
    archiveName: string;

    @Parameter(RugParameters.GroupId)
    groupId: string;

    @Parameter({
        displayName: "Rug Archive Version",
        description: "initial version of the project, e.g., 1.2.3",
        pattern: Pattern.semantic_version,
        validInput: "a valid semantic version, http://semver.org",
        minLength: 1,
        maxLength: 100,
        required: false,
    })
    version: string = "0.1.0";

    edit(project: Project) {
        if (IsRugArchive(project)) {
            return;
        }

        const manifestPath = ".atomist/manifest.yml";
        project.copyEditorBackingFileOrFail(manifestPath);

        let eng: PathExpressionEngine = project.context().pathExpressionEngine();

        let manifestPE = new PathExpression<Project, File>("/*[@name='.atomist']/*[@name='manifest.yml']");
        let manifest: File = eng.scalar(project, manifestPE);
        manifest.regexpReplace("(?m)^group:.*", 'group: "' + this.groupId + '"');
        manifest.regexpReplace("(?m)^artifact:.*", 'artifact: "' + this.archiveName + '"');
        manifest.regexpReplace("(?m)^version:.*", 'version: "' + this.version + '"');
        manifest.regexpReplace("(?m)^dependencies:\s*\n(\s*-.*\n)*", "dependencies:\n");
        manifest.regexpReplace("(?m)^extensions:\s*\n(\s*-.*\n)*", "extensions:\n");
        manifest.regexpReplace("(?m)^repo:.*\n", "");
        manifest.regexpReplace("(?m)^branch:.*\n", "");
        manifest.regexpReplace("(?m)^sha:.*\n", "");
        manifest.regexpReplace("(?m)^---\n", "");
        manifest.regexpReplace("(?m)^\n", "");
    }
}

export const addManifestYml = new AddManifestYml();<commit_msg>Build manifest rather than clean<commit_after>/*
 * Copyright © 2017 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { EditProject } from '@atomist/rug/operations/ProjectEditor';
import { Project } from '@atomist/rug/model/Project';
import { File } from '@atomist/rug/model/File';
import { Pattern } from '@atomist/rug/operations/RugOperation';
import { Editor, Parameter, Tags } from '@atomist/rug/operations/Decorators';

import { IsRugArchive } from './RugEditorsPredicates';
import { RugParameters } from './RugParameters';

@Editor("AddManifestYml", "adds a Rug archive manifest")
@Tags("rug", "atomist")
export class AddManifestYml implements EditProject {

    @Parameter({
        displayName: "Rug Archive Name",
        description: "name of your new Rug Archive, typically the same as the repo name",
        pattern: Pattern.project_name,
        validInput: "a valid GitHub repo name containing only alphanumeric, ., -, and _ characters",
        minLength: 1,
        maxLength: 100
    })
    archiveName: string;

    @Parameter(RugParameters.GroupId)
    groupId: string;

    @Parameter({
        displayName: "Rug Archive Version",
        description: "initial version of the project, e.g., 1.2.3",
        pattern: Pattern.semantic_version,
        validInput: "a valid semantic version, http://semver.org",
        minLength: 1,
        maxLength: 100,
        required: false,
    })
    version: string = "0.1.0";

    edit(project: Project) {
        if (IsRugArchive(project)) {
            return;
        }

        const manifestPath = ".atomist/manifest.yml";
        project.copyEditorBackingFileOrFail(manifestPath);

        let manifest: File = project.findFile(".atomist/manifest.yml");
        let lines: string[] = manifest.content().split("\n");
        let newLines: string[] = [
            `group: "${this.groupId}"`,
            `artifact: "${this.archiveName}"`,
            `version: "${this.version}"`
        ];
        for (let l of lines) {
            if (/^requires:/.test(l)) {
                newLines.push(l);
            }
        }
        manifest.setContent(newLines.join("\n"));
    }
}

export const addManifestYml = new AddManifestYml();
{'fraction_non_alphanumeric': 0.13687895965321775, 'fraction_numerical': 0.007335778592864288, 'mean_word_length': 3.1429558011049723, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7639452', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1669, 'n_words': 490}