Skip to content

Commit 56d476e

Browse files
author
Stewart Miles
committed
Moved file URI conversion and escaping to GradleWrapper
Moved file URI conversion and escaping to GradleWrapper so that it can be reused by other modules. Bug: 135269831, 135273820 Change-Id: I576b0d8d1eb6ae88ebd3c6a7d82ad04e57421601
1 parent 27a0bf5 commit 56d476e

File tree

3 files changed

+54
-41
lines changed

3 files changed

+54
-41
lines changed

source/PlayServicesResolver/src/GradleResolver.cs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,6 @@ internal static Dictionary<string, string> DependenciesToPackageSpecs(
121121
return sourcesByPackageSpec;
122122
}
123123

124-
// Special characters that should not be escaped in URIs for Gradle property values.
125-
private static HashSet<string> GradleUriExcludeEscapeCharacters = new HashSet<string> {
126-
":"
127-
};
128-
129124
/// <summary>
130125
/// Convert a repo path to a valid URI.
131126
/// If the specified repo is a local directory and it doesn't exist, search the project
@@ -145,40 +140,31 @@ internal static string RepoPathToUri(string repoPath, string sourceLocation=null
145140
if (repoPath.StartsWith(PlayServicesSupport.SdkVariable)) return null;
146141
// Since we need a URL, determine whether the repo has a scheme. If not,
147142
// assume it's a local file.
148-
bool validScheme = false;
149143
foreach (var scheme in new [] { "file:", "http:", "https:" }) {
150-
validScheme |= repoPath.StartsWith(scheme);
144+
if (repoPath.StartsWith(scheme)) return GradleWrapper.EscapeUri(repoPath);
151145
}
152-
if (!validScheme) {
153-
// If the directory isn't found, it is possible the user has moved the repository
154-
// in the project, so try searching for it.
155-
string searchDir = "Assets" + Path.DirectorySeparatorChar;
156-
if (!Directory.Exists(repoPath) &&
157-
FileUtils.NormalizePathSeparators(repoPath.ToLower()).StartsWith(
158-
searchDir.ToLower())) {
159-
var foundPath = FileUtils.FindPathUnderDirectory(
160-
searchDir, repoPath.Substring(searchDir.Length));
161-
string warningMessage;
162-
if (!String.IsNullOrEmpty(foundPath)) {
163-
repoPath = searchDir + foundPath;
164-
warningMessage = String.Format(
165-
"{0}: Repo path '{1}' does not exist, will try using '{2}' instead.",
166-
sourceLocation, repoPath, foundPath);
167-
} else {
168-
warningMessage = String.Format(
169-
"{0}: Repo path '{1}' does not exist.", sourceLocation, repoPath);
170-
}
171-
PlayServicesResolver.Log(warningMessage, level: LogLevel.Warning);
172-
}
173146

174-
repoPath = PlayServicesResolver.FILE_SCHEME +
175-
FileUtils.PosixPathSeparators(Path.GetFullPath(repoPath));
147+
// If the directory isn't found, it is possible the user has moved the repository
148+
// in the project, so try searching for it.
149+
string searchDir = "Assets" + Path.DirectorySeparatorChar;
150+
if (!Directory.Exists(repoPath) &&
151+
FileUtils.NormalizePathSeparators(repoPath.ToLower()).StartsWith(
152+
searchDir.ToLower())) {
153+
var foundPath = FileUtils.FindPathUnderDirectory(
154+
searchDir, repoPath.Substring(searchDir.Length));
155+
string warningMessage;
156+
if (!String.IsNullOrEmpty(foundPath)) {
157+
repoPath = searchDir + foundPath;
158+
warningMessage = String.Format(
159+
"{0}: Repo path '{1}' does not exist, will try using '{2}' instead.",
160+
sourceLocation, repoPath, foundPath);
161+
} else {
162+
warningMessage = String.Format(
163+
"{0}: Repo path '{1}' does not exist.", sourceLocation, repoPath);
164+
}
165+
PlayServicesResolver.Log(warningMessage, level: LogLevel.Warning);
176166
}
177-
// Escape the URI to handle special characters like spaces and percent escape
178-
// all characters that are interpreted by gradle.
179-
return GradleWrapper.EscapeGradlePropertyValue(
180-
Uri.EscapeUriString(repoPath), escapeFunc: Uri.EscapeDataString,
181-
charactersToExclude: GradleUriExcludeEscapeCharacters);
167+
return GradleWrapper.EscapeUri(GradleWrapper.PathToFileUri(repoPath));
182168
}
183169

184170
/// <summary>

source/PlayServicesResolver/src/GradleWrapper.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,37 @@ public static string GenerateGradleProperties(Dictionary<string, string> propert
223223
}
224224
return String.Join("\n", lines.ToArray());
225225
}
226+
227+
/// <summary>
228+
/// File scheme that can be concatenated with an absolute path on the local filesystem.
229+
/// </summary>
230+
public const string FILE_SCHEME = "file:///";
231+
232+
/// <summary>
233+
/// Convert a local filesystem path to a URI.
234+
/// </summary>
235+
/// <param name="localPath">Path to convert.</param>
236+
/// <returns>File URI.</returns>
237+
public static string PathToFileUri(string localPath) {
238+
return FILE_SCHEME + FileUtils.PosixPathSeparators(Path.GetFullPath(localPath));
239+
}
240+
241+
// Special characters that should not be escaped in URIs for Gradle property values.
242+
private static HashSet<string> GradleUriExcludeEscapeCharacters = new HashSet<string> {
243+
":"
244+
};
245+
246+
/// <summary>
247+
/// Escape a URI so that it can be passed to Gradle.
248+
/// </summary>
249+
/// <param name="uri">URI to escape.</param>
250+
/// <returns>Escaped URI.</returns>
251+
public static string EscapeUri(string uri) {
252+
// Escape the URI to handle special characters like spaces and percent escape
253+
// all characters that are interpreted by gradle.
254+
return GradleWrapper.EscapeGradlePropertyValue(
255+
Uri.EscapeUriString(uri), escapeFunc: Uri.EscapeDataString,
256+
charactersToExclude: GradleUriExcludeEscapeCharacters);
257+
}
226258
}
227259
}

source/PlayServicesResolver/src/PlayServicesResolver.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,11 +1864,6 @@ public static IList<KeyValuePair<string, string>> GetRepos(
18641864
return GradleResolver.DependenciesToRepoUris(GetOrReadDependencies(dependencies));
18651865
}
18661866

1867-
/// <summary>
1868-
/// File scheme that can be concatenated with an absolute path on the local filesystem.
1869-
/// </summary>
1870-
internal const string FILE_SCHEME = "file:///";
1871-
18721867
/// <summary>
18731868
/// Get the included dependency repos as lines that can be included in a Gradle file.
18741869
/// </summary>
@@ -1887,7 +1882,7 @@ internal static IList<string> GradleMavenReposLines(ICollection<Dependency> depe
18871882
lines.Add(String.Format(
18881883
" def unityProjectPath = \"{0}\" + " +
18891884
"file(rootProject.projectDir.path + \"/../../\").absolutePath",
1890-
FILE_SCHEME));
1885+
GradleWrapper.FILE_SCHEME));
18911886
lines.Add(" maven {");
18921887
lines.Add(" url \"https://maven.google.com\"");
18931888
lines.Add(" }");

0 commit comments

Comments
 (0)