Skip to content

Commit

Permalink
Add license header for code generated by tsp emitter (#1381)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolauli authored Sep 25, 2024
1 parent 86c1ae7 commit 5d5411f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
29 changes: 28 additions & 1 deletion packages/typespec-powershell/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,31 @@ nuspec: "./{module-name}.nuspec"

# misc configurations
skip-model-cmdlets: false
module-version: 0.1.0
module-version: 0.1.0

# License configurations
header-definitions:
warning: Changes may cause incorrect behavior and will be lost if the code is regenerated.

default: "Code generated by Microsoft (R) AutoRest Code Generator (autorest: {core}, generator: {generator})"

no-version: Code generated by Microsoft (R) AutoRest Code Generator.

apache: |
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.
mit: Licensed under the MIT License. See License.txt in the project root for license information.

mit-small: Licensed under the MIT License.

microsoft: Copyright (c) Microsoft Corporation. All rights reserved.

pwsh-license-header: MICROSOFT_APACHE_NO_VERSION
10 changes: 5 additions & 5 deletions powershell/plugins/llcsharp-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export async function llcsharpV2(service: Host | TspHost, state?: ModelState<Pws
await project.writeFiles(async (fname, content) => project.state.writeFile(join(project.apifolder, fname), applyOverrides(content, project.overrides), undefined, 'source-file-csharp'));

// recursive copy resources
await copyResources(join(resources, 'runtime', 'csharp', 'client'), async (fname, content) => project.state.writeFile(join(project.runtimefolder, fname), content, undefined, 'source-file-csharp'), project.overrides);
await copyResources(join(resources, 'runtime', 'csharp', 'pipeline'), async (fname, content) => project.state.writeFile(join(project.runtimefolder, fname), content, undefined, 'source-file-csharp'), project.overrides, transformOutput);
await copyResources(join(resources, 'runtime', 'csharp', 'client'), async (fname, content) => project.state.writeFile(join(project.runtimefolder, fname), content, undefined, 'source-file-other'), project.overrides);
await copyResources(join(resources, 'runtime', 'csharp', 'pipeline'), async (fname, content) => project.state.writeFile(join(project.runtimefolder, fname), content, undefined, 'source-file-other'), project.overrides, transformOutput);
// Note:
// we are using the Carbon.Json library, but we don't *really* want to expose that as public members where we don't have to
// and I don't want to make code changes in the source repository, so I can keep merging from upstream as simple as possible.
// so, we're converting as much as possible to internal, and exposing only what must be exposed to make the code compile.

await copyResources(join(resources, 'runtime', 'csharp', 'json'), async (fname, content) => project.state.writeFile(join(project.runtimefolder, fname), content, undefined, 'source-file-csharp'), {
await copyResources(join(resources, 'runtime', 'csharp', 'json'), async (fname, content) => project.state.writeFile(join(project.runtimefolder, fname), content, undefined, 'source-file-other'), {
...project.overrides,
'public': 'internal',
'internal (.*) class JsonNumber': 'public $1 class JsonNumber',
Expand Down Expand Up @@ -68,11 +68,11 @@ export async function llcsharpV2(service: Host | TspHost, state?: ModelState<Pws
});

if (project.xmlSerialization) {
await copyResources(join(resources, 'runtime', 'csharp', 'xml'), async (fname, content) => project.state.writeFile(join(project.runtimefolder, fname), content, undefined, 'source-file-csharp'), project.overrides);
await copyResources(join(resources, 'runtime', 'csharp', 'xml'), async (fname, content) => project.state.writeFile(join(project.runtimefolder, fname), content, undefined, 'source-file-other'), project.overrides);
}

if (project.azure) {
await copyResources(join(resources, 'runtime', 'csharp.azure'), async (fname, content) => project.state.writeFile(join(project.runtimefolder, fname), content, undefined, 'source-file-csharp'), project.overrides);
await copyResources(join(resources, 'runtime', 'csharp.azure'), async (fname, content) => project.state.writeFile(join(project.runtimefolder, fname), content, undefined, 'source-file-other'), project.overrides);
}

} catch (E) {
Expand Down
4 changes: 2 additions & 2 deletions powershell/plugins/powershell-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function copyRequiredFiles(project: Project) {
join(project.runtimeFolder, fname),
content,
undefined,
sourceFileCSharp
'source-file-other'
),
project.overrides,
transformOutput
Expand All @@ -68,7 +68,7 @@ async function copyRequiredFiles(project: Project) {
join(project.utilsFolder, fname),
content,
undefined,
sourceFileCSharp
'source-file-other'
),
project.overrides,
transformOutput
Expand Down
5 changes: 5 additions & 0 deletions powershell/utils/tsp-host.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Message } from '@autorest/extension-base';
import { comment } from '@azure-tools/codegen';
import { writeFileSync, existsSync, mkdirSync } from 'fs';
import { dirname } from 'path';
export interface TspWriteFileOptions {
Expand Down Expand Up @@ -65,6 +66,10 @@ export class TspHostImpl implements TspHost {
if (artifactType === 'binary-file') {
writeFileSync(filename, Buffer.from(content, 'base64'));
} else {
if (artifactType === 'source-file-csharp' && filename.endsWith('.cs')) {
const header = comment('Copyright (c) Microsoft Corporation. All rights reserved.\nLicensed under the MIT License. See License.txt in the project root for license information.\nChanges may cause incorrect behavior and will be lost if the code is regenerated.', '//');
content = header + '\n' + content;
}
writeFileSync(filename, content);
}
}
Expand Down

0 comments on commit 5d5411f

Please sign in to comment.