walk up parent folders to find available mvnw (#460)

Signed-off-by: Yan Zhang <yanzh@microsoft.com>
This commit is contained in:
Yan Zhang 2020-01-22 15:11:45 +08:00 коммит произвёл GitHub
Родитель cc59ea34af
Коммит e81fedee1c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 41 добавлений и 21 удалений

Просмотреть файл

@ -16,8 +16,8 @@
"contributes.commands.maven.project.showDependencies": "Show dependencies",
"contributes.views.explorer.mavenProjects": "Maven Projects",
"configuration.maven.excludedFolders": "Specifies file path pattern of folders to exclude while searching for Maven projects.",
"configuration.maven.executable.preferMavenWrapper": "Specifies whether you prefer to use Maven wrapper. If true, it tries using 'mvnw' in root folder by default if the file configuration exists. Otherwise it tries 'mvn' in PATH instead.",
"configuration.maven.executable.path": "Specifies absolute path of your mvn executable. When this value is empty, it tries using 'mvn' or 'mvnw' according to value of 'maven.executable.preferMavenWrapper'. Note that a relative path is not suggested, but if you do specify one, the absolute path will be resolved from your workspace root folder (if exists). ",
"configuration.maven.executable.preferMavenWrapper": "Specifies whether you prefer to use Maven wrapper. If true, it tries using 'mvnw' by walking up the parent folders. If false, or 'mvnw' is not found, it tries 'mvn' in PATH instead.",
"configuration.maven.executable.path": "Specifies absolute path of your 'mvn' executable. When this value is empty, it tries using 'mvn' or 'mvnw' according to the value of 'maven.executable.preferMavenWrapper'.",
"configuration.maven.executable.options": "Specifies default options for all mvn commands.",
"configuration.maven.pomfile.autoUpdateEffectivePOM": "Specifies whether to update effective-pom automatically whenever changes detected.",
"configuration.maven.pomfile.globPattern": "Specifies the glob pattern used to look for pom.xml files.",

Просмотреть файл

@ -16,8 +16,8 @@
"contributes.commands.maven.project.showDependencies": "显示所有依赖",
"contributes.views.explorer.mavenProjects": "Maven 项目",
"configuration.maven.excludedFolders": "指定搜索 Maven 项目时要排除的文件夹。",
"configuration.maven.executable.preferMavenWrapper": "指定是否优先使用 Maven Wrapper。如果为 true则尝试使用根文件夹下的 mvnw 作为可执行文件;否则尝试使用系统 PATH 中的 mvn。",
"configuration.maven.executable.path": "指定mvn可执行文件的绝对路径。当此值为空时它会根据 maven.executable.preferMavenWrapper 的值尝试使用 mvn 或 mvnw 。请注意,不建议使用相对路径,但如果指定了相对路径,则将从工作区根文件夹(如果存在)中解析绝对路径。",
"configuration.maven.executable.preferMavenWrapper": "指定是否优先使用 Maven Wrapper。如果为 true则尝试向上遍历父文件夹寻找 mvnw 作为可执行文件;如果为 false或者找不到 mvnw则尝试使用系统 PATH 中的 mvn。",
"configuration.maven.executable.path": "指定mvn可执行文件的绝对路径。当此值为空时它会根据 maven.executable.preferMavenWrapper 的值尝试使用 mvn 或 mvnw 。",
"configuration.maven.executable.options": "指定所有mvn命令的默认选项。",
"configuration.maven.pomfile.autoUpdateEffectivePOM": "指定是否自动更新有效的 POM。",
"configuration.maven.pomfile.globPattern": "指定用于查找 POM 文件的 glob 模式。",

Просмотреть файл

@ -39,14 +39,15 @@ export async function pluginDescription(pluginId: string, pomPath: string): Prom
}
async function executeInBackground(mvnArgs: string, pomfile?: string): Promise<any> {
const workspaceFolder: vscode.WorkspaceFolder | undefined = pomfile ? vscode.workspace.getWorkspaceFolder(vscode.Uri.file(pomfile)) : undefined;
const mvn: string | undefined = await getMaven(workspaceFolder);
const mvn: string | undefined = await getMaven(pomfile);
if (mvn === undefined) {
await promptToSettingMavenExecutable();
return undefined;
}
const command: string = wrappedWithQuotes(mvn);
// TODO: re-visit cwd
const workspaceFolder: vscode.WorkspaceFolder | undefined = pomfile ? vscode.workspace.getWorkspaceFolder(vscode.Uri.file(pomfile)) : undefined;
const cwd: string | undefined = workspaceFolder ? path.resolve(workspaceFolder.uri.fsPath, mvn, "..") : undefined;
const userArgs: string | undefined = Settings.Executable.options(pomfile);
const matched: RegExpMatchArray | null = [mvnArgs, userArgs].filter(Boolean).join(" ").match(/(?:[^\s"]+|"[^"]*")+/g); // Split by space, but ignore spaces in quotes
@ -95,7 +96,7 @@ export async function executeInTerminal(options: {
}): Promise<vscode.Terminal | undefined> {
const { command, mvnPath, pomfile, cwd, env, terminalName } = options;
const workspaceFolder: vscode.WorkspaceFolder | undefined = pomfile ? vscode.workspace.getWorkspaceFolder(vscode.Uri.file(pomfile)) : undefined;
const mvn: string | undefined = mvnPath ? mvnPath : await getMaven(workspaceFolder);
const mvn: string | undefined = mvnPath ? mvnPath : await getMaven(pomfile);
if (mvn === undefined) {
await promptToSettingMavenExecutable();
return undefined;
@ -116,24 +117,45 @@ export async function executeInTerminal(options: {
return terminal;
}
export async function getMaven(workspaceFolder?: vscode.WorkspaceFolder): Promise<string | undefined> {
const workspaceFolderUri: vscode.Uri | undefined = workspaceFolder !== undefined ? workspaceFolder.uri : undefined;
const mvnPathFromSettings: string | undefined = Settings.Executable.path(workspaceFolderUri);
export async function getMaven(pomPath?: string): Promise<string | undefined> {
const mvnPathFromSettings: string | undefined = Settings.Executable.path(pomPath);
if (mvnPathFromSettings) {
return mvnPathFromSettings;
}
const preferMavenWrapper: boolean = Settings.Executable.preferMavenWrapper(workspaceFolderUri);
const localMvnwPath: string | undefined = workspaceFolderUri && path.join(workspaceFolderUri.fsPath, "mvnw");
if (preferMavenWrapper && localMvnwPath && await fse.pathExists(localMvnwPath)) {
return localMvnwPath;
} else {
return await defaultMavenExecutable();
const preferMavenWrapper: boolean = Settings.Executable.preferMavenWrapper(pomPath);
if (preferMavenWrapper && pomPath) {
const localMvnwPath: string | undefined = await getLocalMavenWrapper(path.dirname(pomPath));
if (localMvnwPath) {
return localMvnwPath;
}
}
return await defaultMavenExecutable();
}
export function getEmbeddedMavenWrapper(): string {
return getPathToExtensionRoot("resources", "maven-wrapper", "mvnw");
const mvnw: string = isWin() ? "mvnw.cmd" : "mvnw";
return getPathToExtensionRoot("resources", "maven-wrapper", mvnw);
}
async function getLocalMavenWrapper(projectFolder: string): Promise<string | undefined> {
const mvnw: string = isWin() ? "mvnw.cmd" : "mvnw";
const mvnwProperties: string = path.join(".mvn", "wrapper", "maven-wrapper.properties");
// walk up parent folders
let current: string = projectFolder;
while (path.basename(current)) {
const potentialMvnwPath: string = path.join(current, mvnw);
if (await fse.pathExists(potentialMvnwPath)) {
const potentialMvnwPropsPath: string = path.join(current, mvnwProperties);
if (await fse.pathExists(potentialMvnwPropsPath)) {
return potentialMvnwPath;
}
}
current = path.dirname(current);
}
return undefined;
}
async function defaultMavenExecutable(): Promise<string> {
@ -215,5 +237,5 @@ async function browseForMavenBinary(): Promise<string | undefined> {
}
function isWin(): boolean {
return /^win/.test(process.platform);
return process.platform.startsWith("win");
}

Просмотреть файл

@ -156,7 +156,7 @@
"no-relative-imports": false,
"no-require-imports": true,
"no-shadowed-variable": true,
"no-suspicious-comment": true,
"no-suspicious-comment": false,
"no-typeof-undefined": true,
"no-unnecessary-field-initialization": true,
"no-unnecessary-local-variable": true,
@ -183,9 +183,7 @@
"typedef": [
true,
"call-signature",
// "arrow-call-signature",
"parameter",
// "arrow-parameter",
"property-declaration",
"variable-declaration",
"member-variable-declaration"