Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use dynamic license header characters per language #96

Merged
merged 2 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions tools/quicktype-wrapper/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tools/quicktype-wrapper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
// limitations under the License.

import {readFileSync, writeFileSync} from 'fs';
import {HEADER} from './license';
import {
jsonschema2languageFiles,
LANGUAGE,
LANGUAGE_EXT,
TARGET_LANGUAGE,
LANGUAGE_LICENSE_HEADER,
QtMultifileResult,
} from './quickstype';
const {argv} = require('yargs');
Expand Down Expand Up @@ -144,7 +144,7 @@ if (!module.parent) {
let fileContentsMaybeWithLicenseHeader = genFileContents;
// Optionally add license headers
if (!NO_LICENSE) {
fileContentsMaybeWithLicenseHeader = `${HEADER}\n${fileContentsMaybeWithLicenseHeader}`;
fileContentsMaybeWithLicenseHeader = `${LANGUAGE_LICENSE_HEADER[L]}\n${fileContentsMaybeWithLicenseHeader}`;
}

// Find the relative path
Expand Down
41 changes: 28 additions & 13 deletions tools/quicktype-wrapper/src/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,33 @@
// limitations under the License.

const YEAR = (new Date).getFullYear();
export const HEADER = `// Copyright ${YEAR} Google LLC
//
// 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.

/**
* Gets the Apache License header with a custom character prefix.
* Used to create headers with different comment characters.
* @param {string} C The character prefix before the license.
*/
const HEADER = (C: string) =>
`${C} Copyright ${YEAR} Google LLC
${C}
${C} Licensed under the Apache License, Version 2.0 (the "License");
${C} you may not use this file except in compliance with the License.
${C} You may obtain a copy of the License at
${C}
${C} http://www.apache.org/licenses/LICENSE-2.0
${C}
${C} Unless required by applicable law or agreed to in writing, software
${C} distributed under the License is distributed on an "AS IS" BASIS,
${C} WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
${C} See the License for the specific language governing permissions and
${C} limitations under the License.`;

export const HASH_HEADER = HEADER('#') + "\n";
export const SINGLE_LINE_HEADER = HEADER('//') + "\n";
export const DASH_LINE_HEADER = HEADER('--') + "\n";
export const MULTI_LINE_HEADER =
`/**
${HEADER(' *')}
*/
`;
// Note, newline is important here.
21 changes: 21 additions & 0 deletions tools/quicktype-wrapper/src/quickstype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SerializedRenderResult,
FetchingJSONSchemaStore,
} from 'quicktype-core';
import {HASH_HEADER, DASH_LINE_HEADER, MULTI_LINE_HEADER, SINGLE_LINE_HEADER} from './license';
// Interface not exported in top-level 'quicktype-core': https://github.com/quicktype/quicktype/pull/1565
import {MultiFileRenderResult} from '../node_modules/quicktype-core/dist/TargetLanguage';

Expand Down Expand Up @@ -65,6 +66,26 @@ export const LANGUAGE_EXT = {
PIKE: 'pike',
HASKELL: 'hs',
};
// Expected license reference: https://github.com/google/addlicense/tree/master/testdata/expected
export const LANGUAGE_LICENSE_HEADER = {
CSHARP: SINGLE_LINE_HEADER,
JAVA: MULTI_LINE_HEADER,
PYTHON: HASH_HEADER,
RUST: SINGLE_LINE_HEADER,
CRYSTAL: HASH_HEADER,
RUBY: HASH_HEADER,
GOLANG: SINGLE_LINE_HEADER,
CPLUSPLUS: SINGLE_LINE_HEADER,
ELM: DASH_LINE_HEADER,
SWIFT: SINGLE_LINE_HEADER,
OBJECTIVEC: SINGLE_LINE_HEADER,
TYPESCRIPT: MULTI_LINE_HEADER,
JAVASCRIPT: MULTI_LINE_HEADER,
KOTLIN: MULTI_LINE_HEADER,
DART: SINGLE_LINE_HEADER,
PIKE: SINGLE_LINE_HEADER,
HASKELL: DASH_LINE_HEADER,
};

/**
* Uses quicktype to generate a language file.
Expand Down