use remix wildcard

pull/5370/head
yann300 5 months ago
parent 47a5d97872
commit d9e6adca2f
  1. 3
      libs/remix-core-plugin/src/lib/compiler-content-imports.ts
  2. 3
      libs/remix-ui/workspace/src/lib/actions/index.ts
  3. 50
      libs/remix-ui/workspace/src/lib/actions/workspace.ts
  4. 34
      libs/remix-url-resolver/src/github-folder-resolver.ts

@ -215,8 +215,7 @@ export class CompilerImports extends Plugin {
async resolveGithubFolder (url) {
const ghFolder = {}
const token = await this.call('settings', 'get', 'settings/gist-access-token')
await githubFolderResolver(url, ghFolder, token)
await githubFolderResolver(url, ghFolder, 3)
return ghFolder
}
}

@ -29,6 +29,7 @@ export type UrlParametersType = {
address: string
opendir: string,
blockscout: string,
ghfolder: string
}
const basicWorkspaceInit = async (workspaces: { name: string; isGitRepo: boolean; }[], workspaceProvider) => {
@ -80,7 +81,7 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
plugin.setWorkspace({ name, isLocalhost: false })
dispatch(setCurrentWorkspace({ name, isGitRepo: false }))
await loadWorkspacePreset('gist-template')
} else if (params.code || params.url || params.shareCode) {
} else if (params.code || params.url || params.shareCode || params.ghfolder) {
await createWorkspaceTemplate('code-sample', 'code-template')
plugin.setWorkspace({ name: 'code-sample', isLocalhost: false })
dispatch(setCurrentWorkspace({ name: 'code-sample', isGitRepo: false }))

@ -285,35 +285,35 @@ export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDe
await workspaceProvider.set(path, content)
}
if (params.url) {
if (params.ghfolder === 'true') {
const files = await plugin.call('contentImport', 'resolveGithubFolder', params.url)
console.log(files)
for (const [path, content] of Object.entries(files)) {
await workspaceProvider.set(path, content)
}
} else {
const data = await plugin.call('contentImport', 'resolve', params.url)
path = data.cleanUrl
content = data.content
try {
content = JSON.parse(content) as any
if (content.language && content.language === 'Solidity' && content.sources) {
const standardInput: JSONStandardInput = content as JSONStandardInput
for (const [fname, source] of Object.entries(standardInput.sources)) {
await workspaceProvider.set(fname, source.content)
}
return Object.keys(standardInput.sources)[0]
} else {
// preserve JSON whitespace if this isn't a Solidity compiler JSON-input-output file
content = data.content
await workspaceProvider.set(path, content)
const data = await plugin.call('contentImport', 'resolve', params.url)
path = data.cleanUrl
content = data.content
try {
content = JSON.parse(content) as any
if (content.language && content.language === 'Solidity' && content.sources) {
const standardInput: JSONStandardInput = content as JSONStandardInput
for (const [fname, source] of Object.entries(standardInput.sources)) {
await workspaceProvider.set(fname, source.content)
}
} catch (e) {
console.log(e)
return Object.keys(standardInput.sources)[0]
} else {
// preserve JSON whitespace if this isn't a Solidity compiler JSON-input-output file
content = data.content
await workspaceProvider.set(path, content)
}
}
} catch (e) {
console.log(e)
await workspaceProvider.set(path, content)
}
}
if (params.ghfolder) {
const files = await plugin.call('contentImport', 'resolveGithubFolder', params.ghfolder)
console.log(files)
for (const [path, content] of Object.entries(files)) {
await workspaceProvider.set(path, content)
}
}
return path
} catch (e) {
console.error(e)

@ -1,35 +1,29 @@
// eslint-disable-next-line no-unused-vars
import axios, { AxiosResponse } from 'axios'
export const githubFolderResolver = async (url, obj = {}, token) => {
const child = await pullFolder(url, token)
export const githubFolderResolver = async (url, obj = {}, maxDepth, depth?, rootPath?) => {
depth = depth ? depth : 0
const child = await pullFolder(url)
depth = depth++
const urlObj = new URL(url);
const pathname = urlObj.pathname;
const pathParts = pathname.split('/');
const folderPath = pathParts.slice(5).join('/');
rootPath = rootPath || folderPath
for (const item of child) {
console.log(item)
if (item.type === 'file') {
const response: AxiosResponse = await axios.get(item.download_url, { transformResponse: res => res })
obj[item.path] = response.data
} else {
obj[item.path.replace(rootPath, '')] = response.data
} else if (maxDepth > depth) {
// dir
await githubFolderResolver(item.html_url, obj, token)
await githubFolderResolver(item.html_url, obj, maxDepth, depth, rootPath)
}
}
return obj
}
const pullFolder = async (url, token) => {
url = new URL(url);
const pathname = url.pathname;
const pathParts = pathname.split('/');
const username = pathParts[1];
const repo = pathParts[2];
const folderPath = pathParts.slice(5).join('/');
const apiUrl = `https://api.github.com/repos/${username}/${repo}/contents/${folderPath}`;
const response = await axios.get(apiUrl,
{
headers: {
Authorization: `Bearer ${token}`
}
});
const pullFolder = async (url) => {
const response = await axios.get('https://ghfolderpull.remixproject.org', { params: { ghfolder: url } });
const data = await response.data;
return data
}

Loading…
Cancel
Save