Skip to content

Commit

Permalink
fix(samples): Fix some samples provisioning issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mammerla committed Aug 30, 2024
1 parent 6889d4f commit 23e03d0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/public/data/snippets/editor-samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@
" uiSession.log.debug(`Initializing [${uiSession.extensionContext.extensionInfo.name}] extension`);",
" },",
" {",
" description: ''Farm Generator' Sample Extension',",
" description: 'Farm Generator Sample Extension',",
" notes: 'by Molly',",
" }",
" );",
Expand Down
47 changes: 31 additions & 16 deletions app/src/UX/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ProjectUtilities from "../app/ProjectUtilities";
import WebUtilities from "./WebUtilities";
import ProjectEditorUtilities, { ProjectEditorMode } from "./ProjectEditorUtilities";
import HttpStorage from "../storage/HttpStorage";
import Utilities from "../core/Utilities";

export enum NewProjectTemplateType {
empty,
Expand Down Expand Up @@ -384,9 +385,11 @@ export default class App extends Component<AppProps, AppState> {
if (firstSlash > 1) {
const openToken = openQuery.substring(0, firstSlash).toLowerCase();

const openData = openQuery.substring(firstSlash + 1, openQuery.length);
let openData = openQuery.substring(firstSlash + 1, openQuery.length);

if (openToken === "gp") {
openData = Utilities.ensureNotEndsWithSlash(openData);

this._ensureProjectFromGalleryId(openData, updateContent);
}
}
Expand Down Expand Up @@ -659,7 +662,7 @@ export default class App extends Component<AppProps, AppState> {
}

private async _ensureProjectFromGalleryId(galleryId: string, updateContent?: string) {
if (this.state.carto === undefined) {
if (!this.state || this.state.carto === undefined) {
return;
}

Expand All @@ -673,22 +676,24 @@ export default class App extends Component<AppProps, AppState> {
this._ensureProjectFromGallery(gp, updateContent);
}

private async _ensureProjectFromGallery(project: IGalleryItem, updateContent?: string) {
private async _ensureProjectFromGallery(galleryItem: IGalleryItem, updateContent?: string) {
if (this.state === null || this.state.carto === undefined) {
return;
}

this._ensureProjectFromGitHubTemplate(
project.title,
project.gitHubOwner,
project.gitHubRepoName,
galleryItem.title,
galleryItem.gitHubOwner,
galleryItem.gitHubRepoName,
false,
project.gitHubBranch,
project.gitHubFolder,
project.fileList,
project.id,
project.type === GalleryItemType.codeSample ? project.id : undefined,
updateContent
galleryItem.gitHubBranch,
galleryItem.gitHubFolder,
galleryItem.fileList,
galleryItem.id,
galleryItem.type !== GalleryItemType.project ? galleryItem.id : undefined,
updateContent,
undefined,
galleryItem.type
);
}

Expand All @@ -703,11 +708,13 @@ export default class App extends Component<AppProps, AppState> {
projectId?: string,
sampleId?: string,
updateContent?: string,
description?: string
description?: string,
galleryItemType?: GalleryItemType
) {
const carto = CartoApp.carto;

if (this.state === null || carto === undefined) {
// don't load if we're already actively loading something.
if (this.state === null || carto === undefined || this._loadingMessage || this.state.loadingMessage) {
return;
}

Expand Down Expand Up @@ -745,6 +752,7 @@ export default class App extends Component<AppProps, AppState> {
proj.originalGitHubRepoName === gitHubRepoName &&
proj.originalGitHubBranch === gitHubBranch &&
proj.originalGitHubFolder === gitHubFolder &&
proj.originalSampleId === sampleId &&
updateContent === undefined
) {
this._updateWindowTitle(newMode, proj);
Expand Down Expand Up @@ -774,7 +782,11 @@ export default class App extends Component<AppProps, AppState> {
projectId,
sampleId,
updateContent,
description
undefined,
undefined,
undefined,
description,
galleryItemType
);
}

Expand Down Expand Up @@ -1280,7 +1292,10 @@ export default class App extends Component<AppProps, AppState> {
onProjectSelected={this._handleProjectSelected}
/>
);
} else if (this.state.activeProject.originalSampleId) {
} else if (
this.state.activeProject.originalSampleId &&
this.state.activeProject.getItemByProjectPath("/scripts/ScriptBox.ts")
) {
// show main view (no sidebar) if it's a code sample.
interior = (
<ProjectEditor
Expand Down
2 changes: 1 addition & 1 deletion app/src/UX/ItemGallery.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
}

.ig-notFound {
padding-top: 6px;
padding-top: 9px;
padding-left: 6px;
}

Expand Down
18 changes: 16 additions & 2 deletions app/src/app/ProjectUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,6 @@ export default class ProjectUtilities {
"ModalFormResponse",
],
mc: [
"world",
"system",
"BlockPermutation",
"BlockSignComponent",
Expand Down Expand Up @@ -896,10 +895,23 @@ export default class ProjectUtilities {
"TripWireAfterEvent",
"LeverActionAfterEvent",
"Vector3",
"DimensionLocation",
],
};

static adaptFullSample(content: string) {
const registerFunction = content.indexOf("export function register");

if (registerFunction >= 0) {
let nextParen = content.indexOf("Extension(", registerFunction);

if (nextParen > registerFunction) {
content = content.substring(0, registerFunction + 24) + content.substring(nextParen);
}
}

return content;
}

static adaptSample(sampleContent: string, fileContent: string) {
if (sampleContent.indexOf(" mc.") >= 0 && fileContent.indexOf(" as mc") <= 0) {
sampleContent = sampleContent.replace(/mc./gi, "");
Expand Down Expand Up @@ -1033,6 +1045,8 @@ export default class ProjectUtilities {

if (file !== undefined) {
if (fullScriptBoxReplace && fileName === "ScriptBox.ts") {
snippetInjectContent = ProjectUtilities.adaptFullSample(snippetInjectContent);

file.setContent(snippetInjectContent);
} else {
const type = StorageUtilities.getTypeFromName(file.name);
Expand Down
8 changes: 8 additions & 0 deletions app/src/core/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,14 @@ export default class Utilities {
return pathSegment;
}

static ensureNotEndsWithSlash(pathSegment: string) {
while (pathSegment.length > 0 && pathSegment.endsWith("/")) {
pathSegment = pathSegment.substring(0, pathSegment.length - 1);
}

return pathSegment;
}

static ensureStartsWithBackSlash(pathSegment: string) {
if (!pathSegment.startsWith("\\")) {
pathSegment = "\\" + pathSegment;
Expand Down

0 comments on commit 23e03d0

Please sign in to comment.