|
| 1 | +/********************************************************************* |
| 2 | + * Copyright (c) 2019 Red Hat, Inc. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + **********************************************************************/ |
| 10 | + |
| 11 | +import * as theia from '@theia/plugin'; |
| 12 | +import * as fs from 'fs'; |
| 13 | +import * as path from 'path'; |
| 14 | +export const YEARS_IDENTIFIER = '${Years}'; |
| 15 | +export const AUTHORS_IDENTIFIER = '${Authors}'; |
| 16 | + |
| 17 | +export const MIT_LICENSE_HEADER_CONTENT = `/*\n* Copyright (c) ${YEARS_IDENTIFIER}${AUTHORS_IDENTIFIER}. All rights reserved.\n* @license MIT\n*/\n`; |
| 18 | +export const MIT_LICENSE_HEDER_REGEXP = new RegExp(/.*(Copyright \(c\) ).*(. All rights reserved.\n).*(@license MIT\n).*/); |
| 19 | + |
| 20 | +export const PACKAGE_JSON = 'package.json'; |
| 21 | + |
| 22 | +export const SUPPORTED_EXTENSIONS = ['.js', '.ts']; |
| 23 | +export const SUPPORTED_LICENCE_HEADERS = ['MIT']; |
| 24 | + |
| 25 | +export class LicenseHeader { |
| 26 | + |
| 27 | + constructor(private readonly authors: string, private readonly licenseContent: string, private readonly licenceRegexp: RegExp) { } |
| 28 | + |
| 29 | + getHeader(): string { |
| 30 | + const currentYear = new Date().getFullYear(); |
| 31 | + const spaceAfterYear = this.authors ? ' ' : ''; |
| 32 | + const header = this.licenseContent.replace(YEARS_IDENTIFIER, currentYear + spaceAfterYear).replace(AUTHORS_IDENTIFIER, this.authors || ''); |
| 33 | + return header; |
| 34 | + } |
| 35 | + |
| 36 | + getRegExpr(): RegExp { |
| 37 | + return this.licenceRegexp; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +export function start(context: theia.PluginContext) { |
| 42 | + context.subscriptions.push(theia.workspace.onWillSaveTextDocument(fileSaveEvent => { |
| 43 | + const fileContentPromise = new Promise<void | theia.TextEdit[]>((resolve, reject) => { |
| 44 | + const document = fileSaveEvent.document; |
| 45 | + const fileExt = path.extname(document.uri.fsPath); |
| 46 | + if (!isSupportedValue(fileExt, SUPPORTED_EXTENSIONS)) { |
| 47 | + resolve([]); |
| 48 | + } |
| 49 | + const fileUri = document.uri; |
| 50 | + const workspaceFolder: theia.WorkspaceFolder | theia.Uri | undefined = theia.workspace.getWorkspaceFolder(fileUri); |
| 51 | + let workspaceFolderUri: theia.Uri | undefined; |
| 52 | + if (workspaceFolder) { |
| 53 | + workspaceFolderUri = workspaceFolder instanceof theia.Uri ? workspaceFolder : workspaceFolder.uri; |
| 54 | + } |
| 55 | + |
| 56 | + if (workspaceFolderUri) { |
| 57 | + const packageJsonPath = path.resolve(workspaceFolderUri.fsPath, PACKAGE_JSON); |
| 58 | + if (fs.existsSync(packageJsonPath)) { |
| 59 | + const packageJson = require(packageJsonPath); |
| 60 | + |
| 61 | + if (document && packageJson.license && isSupportedValue(packageJson.license, SUPPORTED_LICENCE_HEADERS)) { |
| 62 | + const fileText = document.getText(); |
| 63 | + |
| 64 | + const textEdits: theia.TextEdit[] = []; |
| 65 | + const mitLicenceHeader = new LicenseHeader(packageJson.author, MIT_LICENSE_HEADER_CONTENT, MIT_LICENSE_HEDER_REGEXP); |
| 66 | + |
| 67 | + if (!mitLicenceHeader.getRegExpr().test(fileText)) { |
| 68 | + const licenceHeaderEdit = theia.TextEdit.insert(document.lineAt(0).range.start, mitLicenceHeader.getHeader()); |
| 69 | + textEdits.push(licenceHeaderEdit); |
| 70 | + } |
| 71 | + resolve(textEdits); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + resolve(); |
| 76 | + }); |
| 77 | + |
| 78 | + fileSaveEvent.waitUntil(fileContentPromise); |
| 79 | + })); |
| 80 | +} |
| 81 | + |
| 82 | +function isSupportedValue(item: string, supportedElems: string[]): boolean { |
| 83 | + return supportedElems.findIndex((supportedElem => supportedElem === item)) >= 0; |
| 84 | +} |
| 85 | + |
| 86 | +export function stop() { |
| 87 | +} |
0 commit comments