UNPKG

@atomist/atomist-sdm

Version:

Atomist SDM to deliver our own projects

163 lines 6.85 kB
"use strict"; /* * Copyright © 2019 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. */ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const automation_client_1 = require("@atomist/automation-client"); const minimatch = require("minimatch"); const GlobPatterns_1 = require("./GlobPatterns"); const RequestedCommitParameters_1 = require("./RequestedCommitParameters"); /** * Default glob pattern matches all C family languages */ let AddHeaderParameters = class AddHeaderParameters extends RequestedCommitParameters_1.RequestedCommitParameters { constructor() { super("Add missing license headers"); this.glob = GlobPatterns_1.CFamilyLanguageSourceFiles; this.onlyChangedFiles = false; this.license = "apache"; } get header() { switch (this.license) { case "apache": return apacheHeader(); default: throw new Error(`'${this.license}' is not a supported license`); } } }; __decorate([ automation_client_1.Parameter({ required: false }), __metadata("design:type", String) ], AddHeaderParameters.prototype, "glob", void 0); __decorate([ automation_client_1.Parameter({ required: false }), __metadata("design:type", String) ], AddHeaderParameters.prototype, "excludeGlob", void 0); __decorate([ automation_client_1.Parameter({ required: false }), __metadata("design:type", Boolean) ], AddHeaderParameters.prototype, "onlyChangedFiles", void 0); __decorate([ automation_client_1.Parameter({ required: false }), __metadata("design:type", String) ], AddHeaderParameters.prototype, "license", void 0); AddHeaderParameters = __decorate([ automation_client_1.Parameters(), __metadata("design:paramtypes", []) ], AddHeaderParameters); exports.AddHeaderParameters = AddHeaderParameters; function apacheHeader() { const year = (new Date()).getFullYear(); return `/* * Copyright © ${year} 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. */ `; } exports.apacheHeader = apacheHeader; /** * CodeTransform that upserts headers into files per [[AddHeaderParameters]]. */ function addHeaderTransform(p, ci) { return __awaiter(this, void 0, void 0, function* () { yield automation_client_1.projectUtils.doWithFiles(p, ci.parameters.glob, (f) => __awaiter(this, void 0, void 0, function* () { if (ci.parameters.excludeGlob && minimatch(f.path, ci.parameters.excludeGlob)) { return; } if (ci.parameters.onlyChangedFiles) { if (ci.push.filesChanged && ci.push.filesChanged.length > 0) { if (!ci.push.filesChanged.includes(f.path)) { return; } } else { return; } } const content = yield f.getContent(); const header = ci.parameters.header; const newContent = upsertHeader(header, content); if (newContent !== content) { yield f.setContent(newContent); } return; })); return p; }); } exports.addHeaderTransform = addHeaderTransform; /** * There are some lines that really need to be at the top. * * If a file starts with '#!/executable/to/run', leave that at the * top. It's invalid to put a comment before it. * * @param content to extract sh-bang line from * @return two-element array, the first element if the sh-bang line or * an empty string if there is no sh-bang line, the second element * is the rest of the content. */ function separatePrefixLines(content) { if (content.startsWith("#!")) { const lines = content.split("\n"); return [lines[0] + "\n", lines.slice(1).join("\n")]; } return ["", content]; } /** * Add or replace begining empty lines and any header comment in * `content` with `header`. * * @param header header that should be upserted into content * @param content current content to be updated * @return updated content that contains header */ function upsertHeader(header, content) { const [prefix, rest] = separatePrefixLines(content); const preamble = prefix + header + "\n"; return preamble + rest.replace(/^(?:\s*\n)?(?:\/\*[\s\S]*?\*\/\s*\n)?/, ""); } exports.upsertHeader = upsertHeader; //# sourceMappingURL=addHeader.js.map