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

project manifest: default source paths #1911

Merged
merged 7 commits into from
Oct 2, 2023
Merged
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
2 changes: 2 additions & 0 deletions packages/cli/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@
"lib_project_app_uri_support": "The app command supports only filesystem URIs for plugins. Examples:",
"lib_project_imported_plugin_manifest_not_found": "No manifest found for imported plugin namespace `{namespace}` at path `{path}`",
"lib_project_invalid_uri": "Invalid URI Received:",
"lib_project_no_default_schema": "Couldn't find schema in default paths. Please specify the schema location in the project manifest.",
"lib_project_no_default_module": "Couldn't find module entry point in default paths. Please specify the module entry point in the project manifest.",
"lib_typescript_notInstalled": "Your project uses typescript, but it's not installed",
"lib_typescript_tsNodeNotInstalled": "Your project uses typescript, but ts-node is not installed",
"lib_watcher_alreadyWatching": "Watcher session is already in progress. Directory: {dir}",
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@
"lib_project_app_uri_support": "The app command supports only filesystem URIs for plugins. Examples:",
"lib_project_imported_plugin_manifest_not_found": "No manifest found for imported plugin namespace `{namespace}` at path `{path}`",
"lib_project_invalid_uri": "Invalid URI Received:",
"lib_project_no_default_schema": "Couldn't find schema in default paths. Please specify the schema location in the project manifest.",
"lib_project_no_default_module": "Couldn't find module entry point in default paths. Please specify the module entry point in the project manifest.",
"lib_typescript_notInstalled": "Your project uses typescript, but it's not installed",
"lib_typescript_tsNodeNotInstalled": "Your project uses typescript, but ts-node is not installed",
"lib_watcher_alreadyWatching": "Watcher session is already in progress. Directory: {dir}",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ export const build: Command = {
async function validateManifestModules(polywrapManifest: PolywrapManifest) {
if (
polywrapManifest.project.type !== "interface" &&
!polywrapManifest.source.module
!polywrapManifest.source?.module
) {
const missingModuleMessage = intlMsg.lib_compiler_missingModule();
throw Error(missingModuleMessage);
}

if (
polywrapManifest.project.type === "interface" &&
polywrapManifest.source.module
polywrapManifest.source?.module
) {
const noInterfaceModule = intlMsg.lib_compiler_noInterfaceModule();
throw Error(noInterfaceModule);
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/lib/SchemaComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class SchemaComposer {
private _abiResolver(
schemaFile: SchemaFile,
importFrom: string,
import_abis?: PolywrapManifest["source"]["import_abis"]
import_abis?: NonNullable<PolywrapManifest["source"]>["import_abis"]
): Promise<WrapAbi | SchemaFile> {
if (Uri.isValidUri(importFrom)) {
return this._resolveUri(importFrom, import_abis);
Expand All @@ -97,7 +97,7 @@ export class SchemaComposer {

private async _resolveUri(
uri: string,
import_abis?: PolywrapManifest["source"]["import_abis"]
import_abis?: NonNullable<PolywrapManifest["source"]>["import_abis"]
): Promise<WrapAbi> {
// Check to see if we have any import redirects that match
if (import_abis) {
Expand Down Expand Up @@ -155,7 +155,7 @@ export class SchemaComposer {

private async _loadGraphqlAbi(
path: string,
import_abis: PolywrapManifest["source"]["import_abis"]
import_abis: NonNullable<PolywrapManifest["source"]>["import_abis"]
): Promise<WrapAbi> {
const schema = fs.readFileSync(path, "utf-8");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function getCodegenOverrides(): CodegenOverrides {

function getGoModulePath(manifest: PolywrapManifest): string {
// Ensure `module: ...` is pointing to a `go.mod` file
const module = manifest.source.module;
const module = manifest.source?.module;
if (!module || module.indexOf("go.mod") === -1) {
throw Error(
intlMsg.lib_wasm_golang_invalidModule({ path: module || "N/A" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PolywrapManifest } from "@polywrap/polywrap-manifest-types-js";
export function getBuildOverrides(): BuildOverrides {
return {
validateManifest: (manifest: PolywrapManifest) => {
const module = manifest.source.module;
const module = manifest.source?.module;

if (module && module.indexOf("Cargo.toml") === -1) {
throw Error(intlMsg.lib_wasm_rust_invalidModule({ path: module }));
Expand Down
15 changes: 12 additions & 3 deletions packages/cli/src/lib/project/AppProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,21 @@ export class AppProject extends Project<AppManifest> {
public async getSchemaNamedPath(): Promise<string> {
const manifest = await this.getManifest();
const dir = this.getManifestDir();
return path.join(dir, manifest.source.schema);
if (!manifest.source?.schema) {
throw new Error(
`No schema path specified in project manifest with name "${manifest.project.name}". This should never happen.`
);
}
return path.isAbsolute(manifest.source.schema)
? manifest.source.schema
: path.join(dir, manifest.source.schema);
}

public async getImportAbis(): Promise<AppManifest["source"]["import_abis"]> {
public async getImportAbis(): Promise<
NonNullable<AppManifest["source"]>["import_abis"]
> {
const manifest = await this.getManifest();
return manifest.source.import_abis || [];
return manifest.source?.import_abis || [];
}

public async getGenerationDirectory(
Expand Down
16 changes: 11 additions & 5 deletions packages/cli/src/lib/project/PluginProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,21 @@ export class PluginProject extends Project<PluginManifest> {
public async getSchemaNamedPath(): Promise<string> {
const manifest = await this.getManifest();
const dir = this.getManifestDir();
return path.join(dir, manifest.source.schema);
if (!manifest.source?.schema) {
throw new Error(
`No schema path specified in project manifest with name "${manifest.project.name}". This should never happen.`
);
}
return path.isAbsolute(manifest.source.schema)
? manifest.source.schema
: path.join(dir, manifest.source.schema);
}

public async getImportAbis(): Promise<
PluginManifest["source"]["import_abis"]
NonNullable<PluginManifest["source"]>["import_abis"]
> {
const manifest = await this.getManifest();
return manifest.source.import_abis || [];
return manifest.source?.import_abis || [];
}

public async getGenerationDirectory(
Expand All @@ -123,7 +130,6 @@ export class PluginProject extends Project<PluginManifest> {
const moduleDirectory = await this.getGenerationDirectory(
generationSubPath
);

// Clean the code generation
resetDir(moduleDirectory);

Expand Down Expand Up @@ -158,7 +164,7 @@ export class PluginProject extends Project<PluginManifest> {
manifest.project.type as PluginManifestLanguage
) ||
// 3. If a module path exists, generate within a "wrap" dir next to it
(manifest.source.module &&
(manifest.source?.module &&
path.join(path.dirname(manifest.source.module), "wrap")) ||
// 4. Use the default
defaultDir;
Expand Down
17 changes: 12 additions & 5 deletions packages/cli/src/lib/project/PolywrapProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,21 @@ export class PolywrapProject extends Project<PolywrapManifest> {
public async getSchemaNamedPath(): Promise<string> {
const manifest = await this.getManifest();
const dir = this.getManifestDir();
return path.join(dir, manifest.source.schema);
if (!manifest.source?.schema) {
throw new Error(
`No schema path specified in project manifest with name "${manifest.project.name}". This should never happen.`
);
}
return path.isAbsolute(manifest.source.schema)
? manifest.source.schema
: path.join(dir, manifest.source.schema);
}

public async getImportAbis(): Promise<
PolywrapManifest["source"]["import_abis"]
NonNullable<PolywrapManifest["source"]>["import_abis"]
> {
const manifest = await this.getManifest();
return manifest.source.import_abis || [];
return manifest.source?.import_abis || [];
}

public async getGenerationDirectory(
Expand Down Expand Up @@ -366,7 +373,7 @@ export class PolywrapProject extends Project<PolywrapManifest> {
> {
const manifest = await this.getManifest();

if (manifest.source.module) {
if (manifest.source?.module) {
return {
moduleFilePath: manifest.source.module,
dir: path.dirname(manifest.source.module).replace("./", ""),
Expand All @@ -389,7 +396,7 @@ export class PolywrapProject extends Project<PolywrapManifest> {
manifest.project.type as PolywrapManifestLanguage
) ||
// 3. If a module path exists, generate within a "wrap" dir next to it
(manifest.source.module &&
(manifest.source?.module &&
path.join(path.dirname(manifest.source.module), "wrap")) ||
// 4. Use the default
defaultDir;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lib/project/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export abstract class Project<TManifest extends AnyProjectManifest> {
public abstract getSchemaNamedPath(): Promise<string>;

public abstract getImportAbis(): Promise<
PolywrapManifest["source"]["import_abis"]
NonNullable<PolywrapManifest["source"]>["import_abis"]
>;

public abstract getGenerationDirectory(
Expand Down
16 changes: 16 additions & 0 deletions packages/cli/src/lib/project/manifests/app/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defaultSchemaPath } from "../defaults";

import { AppManifest } from "@polywrap/polywrap-manifest-types-js";

export function applyAppManifestDefaults(
manifest: AppManifest,
manifestPath: string
): AppManifest {
if (!manifest.source) {
manifest.source = {};
}
if (!manifest.source.schema) {
manifest.source.schema = defaultSchemaPath(manifestPath);
}
return manifest;
}
4 changes: 3 additions & 1 deletion packages/cli/src/lib/project/manifests/app/load.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { displayPath, Logger, logActivity, intlMsg } from "../../../";
import { applyAppManifestDefaults } from "./defaults";

import {
AppManifest,
Expand All @@ -22,7 +23,8 @@ export async function loadAppManifest(
}

try {
const result = deserializeAppManifest(manifest, { logger: logger });
const decoded = deserializeAppManifest(manifest, { logger: logger });
const result = applyAppManifestDefaults(decoded, manifestPath);
return Promise.resolve(result);
} catch (e) {
return Promise.reject(e);
Expand Down
22 changes: 22 additions & 0 deletions packages/cli/src/lib/project/manifests/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { intlMsg } from "../../intl";

import path from "path";
import fs from "fs";

export function defaultSchemaPath(manifestPath: string): string {
const defaultSchemaPaths = [
"polywrap.graphql",
"src/polywrap.graphql",
"schema.graphql",
"src/schema.graphql",
];
const manifestDir = path.dirname(manifestPath);
for (const relPath of defaultSchemaPaths) {
const absPath = path.resolve(manifestDir, relPath);
if (fs.existsSync(absPath)) {
return absPath;
}
}

throw Error(intlMsg.lib_project_no_default_schema());
}
61 changes: 61 additions & 0 deletions packages/cli/src/lib/project/manifests/plugin/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { defaultSchemaPath } from "../defaults";
import { isPluginManifestLanguage, pluginManifestLanguages } from "./languages";
import { intlMsg } from "../../../intl";

import fs from "fs";
import path from "path";
import { PluginManifest } from "@polywrap/polywrap-manifest-types-js";

export function applyPluginManifestDefaults(
manifest: PluginManifest,
manifestPath: string
): PluginManifest {
if (!manifest.source) {
manifest.source = {};
}
if (!manifest.source?.module) {
const language = manifest.project.type;
manifest.source.module = defaultModulePath(language, manifestPath);
}
if (!manifest.source.schema) {
manifest.source.schema = defaultSchemaPath(manifestPath);
}
return manifest;
}

function defaultModulePath(
language: string,
manifestPath: string
): string | undefined {
if (!isPluginManifestLanguage(language)) {
throw Error(
intlMsg.lib_language_unsupportedManifestLanguage({
language: language,
supported: Object.keys(pluginManifestLanguages).join(", "),
})
);
}

let relEntryPoint: string;
if (language === "plugin/typescript") {
relEntryPoint = "src/index.ts";
} else if (language == "plugin/rust") {
relEntryPoint = "Cargo.toml";
} else if (language == "plugin/python") {
relEntryPoint = "src/__init__.py";
} else if (language == "plugin/swift") {
relEntryPoint = "Package.swift";
} else if (language == "plugin/kotlin") {
relEntryPoint = "src/commonMain/kotlin";
} else {
throw Error(intlMsg.lib_project_no_default_module());
}

const manifestDir = path.dirname(manifestPath);
const absEntryPoint = path.resolve(manifestDir, relEntryPoint);
if (fs.existsSync(absEntryPoint)) {
return relEntryPoint;
}

throw Error(intlMsg.lib_project_no_default_module());
}
4 changes: 3 additions & 1 deletion packages/cli/src/lib/project/manifests/plugin/load.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { displayPath, Logger, logActivity, intlMsg } from "../../../";
import { applyPluginManifestDefaults } from "./defaults";

import {
PluginManifest,
Expand Down Expand Up @@ -26,7 +27,8 @@ export async function loadPluginManifest(
}

try {
const result = deserializePluginManifest(manifest, { logger: logger });
const decoded = deserializePluginManifest(manifest, { logger: logger });
const result = applyPluginManifestDefaults(decoded, manifestPath);
return Promise.resolve(result);
} catch (e) {
return Promise.reject(e);
Expand Down
62 changes: 62 additions & 0 deletions packages/cli/src/lib/project/manifests/polywrap/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
isPolywrapManifestLanguage,
polywrapManifestLanguages,
} from "./languages";
import { defaultSchemaPath } from "../defaults";
import { intlMsg } from "../../../intl";

import fs from "fs";
import path from "path";
import { PolywrapManifest } from "@polywrap/polywrap-manifest-types-js";

export function applyPolywrapManifestDefaults(
manifest: PolywrapManifest,
manifestPath: string
): PolywrapManifest {
if (!manifest.source) {
manifest.source = {};
}
if (!manifest.source.module) {
const language = manifest.project.type;
manifest.source.module = defaultModulePath(language, manifestPath);
}
if (!manifest.source.schema) {
manifest.source.schema = defaultSchemaPath(manifestPath);
}
return manifest;
}

function defaultModulePath(
language: string,
manifestPath: string
): string | undefined {
if (!isPolywrapManifestLanguage(language)) {
throw Error(
intlMsg.lib_language_unsupportedManifestLanguage({
language: language,
supported: Object.keys(polywrapManifestLanguages).join(", "),
})
);
}

let relEntryPoint: string;
if (language === "wasm/typescript" || language === "wasm/assemblyscript") {
relEntryPoint = "src/index.ts";
} else if (language == "wasm/rust") {
relEntryPoint = "Cargo.toml";
} else if (language == "wasm/golang") {
relEntryPoint = "go.mod";
} else if (language == "interface") {
return undefined;
} else {
throw Error(intlMsg.lib_project_no_default_module());
}

const manifestDir = path.dirname(manifestPath);
const absEntryPoint = path.resolve(manifestDir, relEntryPoint);
if (fs.existsSync(absEntryPoint)) {
return absEntryPoint;
}

throw Error(intlMsg.lib_project_no_default_module());
}
Loading