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

Add license selector #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
81 changes: 81 additions & 0 deletions scripts/package-lock.json

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

1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"eta": "^1.12.3",
"file-saver": "^2.0.5",
"front-matter": "^4.0.2",
"jszip": "^3.10.1"
}
}
20 changes: 20 additions & 0 deletions scripts/src/lib/Template.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import DownloadIcon from "./DownloadIcon.svelte";
import { nameToModId } from "./template/template";
import { getMinorMinecraftVersion } from "./template/java";
import { licenses, licensesMap } from "./template/license";

let minecraftVersion: string;
let projectName = "Template Mod";
let packageName = "com.example";
let useKotlin = false;
let dataGeneration = false;
let splitSources = true;
let license = "MIT License";

let customModId: string | undefined;
let loading = false;
Expand Down Expand Up @@ -107,6 +109,7 @@
useKotlin,
dataGeneration: dataGeneration && supportsDataGen,
splitSources: splitSources && supportsSplitSources,
license: licensesMap.get(license)!,
};

const zip = new JSZip();
Expand Down Expand Up @@ -202,6 +205,23 @@
</select>
</div>

<div class="form-line">
<h3>Mod License:</h3>
<hr />
<p>
Select the license you wish to use for your mod. For help with choosing a license please visit GitHub's <a href="https://choosealicense.com/" target="_blank" rel="noopener noreferrer">https://choosealicense.com/</a> website.
</p>
<select
id="license"
bind:value={license}
style="width: 200px"
>
{#each licenses as license}
<option value={license.name}>{license.name}</option>
{/each}
</select>
</div>

<hr>
<br>

Expand Down
2 changes: 0 additions & 2 deletions scripts/src/lib/template/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import type { ComputedConfiguration, TemplateWriter } from "./template";

import gitignore from './templates/git/gitignore?raw';
import workflow from './templates/git/workflow.yml?raw';
import license from './templates/git/LICENSE?raw';

export async function addGitFiles(writer: TemplateWriter, config: ComputedConfiguration) {
await writer.write('.gitignore', gitignore);
await writer.write('.github/workflows/build.yml', workflow);
await writer.write('LICENSE', license);
}
80 changes: 80 additions & 0 deletions scripts/src/lib/template/license.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import frontMatter from 'front-matter'
import type { ComputedConfiguration, TemplateWriter } from './template';

const licensesRaw = import.meta.glob("./licenses/*.txt", { as: "raw", eager: true });
const headersRaw = import.meta.glob("./licenses/headers/*.txt", { as: "raw", eager: true });

const currentYear = new Date().getFullYear().toString();

// https://github.com/desktop/desktop/blob/af7b8ae7d1f88ba93e01731cdd7d070ee7ca4a7e/script/build.ts#L16
interface ChooseALicense {
readonly title: string
readonly nickname?: string
readonly "spdx-id": string
}

interface Header {
readonly "spdx-id": string
}

export type License = {
readonly name: string
readonly id: string
readonly body: string
};

export const licenses = Object.values(licensesRaw)
.map(parseLicense)
.sort((a, b) => a.name.localeCompare(b.name));
export const licensesMap = new Map(licenses.map(l => [l.name, l]));
export const licenseHeaders = new Map(Object.values(headersRaw).map(parseHeader));

function parseLicense(content: string): License {
const result = frontMatter<ChooseALicense>(content);

return {
name: result.attributes.nickname || result.attributes.title,
id: result.attributes['spdx-id'],
body: result.body,
};
}

function parseHeader(content: string): [string, string] {
const result = frontMatter<Header>(content);
const headerText = result.body.trim()
return [result.attributes['spdx-id'], headerText]
}

export function fillLicensePlaceholders(text: string): string {
// TODO [fullname]?
// TODO [modname]
return text.replaceAll("[yyyy]", currentYear);
}

export function applyHeader(text: string, license: License): string {
const header = licenseHeaders.get(license.id);

if (header == undefined) {
return text;
}

return commentBlock(fillLicensePlaceholders(header)) + "\n\n" + text;
}

export async function addLicense(writer: TemplateWriter, options: ComputedConfiguration): Promise<void> {
writer.write("LICENSE", fillLicensePlaceholders(options.license.body));
}

/*
* Blah
*/
function commentBlock(header: string): string {
var lines = ["/*"];

for (var line of header.split("\n")) {
lines.push(" * " + line);
}

lines.push(" */")
return lines.join("\n");
}
Loading