-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceIdentifier.cs
More file actions
170 lines (153 loc) · 6.35 KB
/
Copy pathResourceIdentifier.cs
File metadata and controls
170 lines (153 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.IO;
using nyoka_Client;
using static nyoka_Client.Constants;
using static nyoka_Client.ParseUtils;
namespace nyoka_Client
{
public class ResourceIdentifier
{
public string resourceName;
public string version;
public Constants.ResourceType resourceType;
public ResourceIdentifier(string resourceName, Constants.ResourceType resourceType)
{
this.resourceName = resourceName;
this.resourceType = resourceType;
this.version = null;
}
public ResourceIdentifier(string resourceName, Constants.ResourceType resourceType, string version)
{
this.resourceName = resourceName;
this.resourceType = resourceType;
this.version = version;
}
public static ResourceIdentifier generateResourceIdentifier(string resourceStr)
{
string[] splitByAt = resourceStr.Split('@');
string version;
string resourceNameStr;
// If there is no @ symbol in the string to separate name from version
if (splitByAt.Length == 1)
{
version = null; // redundant?
resourceNameStr = splitByAt[0];
//Console.WriteLine(resourceNameStr +"Surbhi");
}
// If there is one @ symbol in the string to separate name from version
else if (splitByAt.Length == 2)
{
version = splitByAt[1];
resourceNameStr = splitByAt[0];
//Console.WriteLine(version );Console.WriteLine(resourceNameStr );
}
// If there is more than one @ symbol in the string
else
{
throw new ArgumentProcessException(
$"Could not process \"{resourceStr}\": Only one @ symbol is permitted in a resource name"
);
}
string resourceName = removeFolderPrefixIfPresent(resourceNameStr);
ResourceType resourceType = inferResourceTypeFromResourceName(resourceName);
if (version != null)
{
// validate version string
validateVersionString(resourceStr, version);
}
validateFileName(resourceName);
return new ResourceIdentifier(resourceName, resourceType, version);
}
private static string removeFolderPrefixIfPresent(string resourceNameStr)
{
if (resourceNameStr.Split("/").Length > 1)
{
int indexOfLastSlash = resourceNameStr.LastIndexOf('/');
//Console.WriteLine(resourceNameStr.Substring(indexOfLastSlash + 1));
return resourceNameStr.Substring(indexOfLastSlash + 1);
}
else
{
return resourceNameStr;
}
}
private static void validateFileName(string resourceName)
{
foreach (char ch in resourceName)
{
// @TODO use regex?
if (!"qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890_-.".Contains(ch))
{
throw new ArgumentProcessException($"Invalid character in file name: \"{ch}\"");
}
}
}
private static ResourceType inferResourceTypeFromResourceName(string resourceName)
{
//Console.WriteLine(FileTypeInference.isDataFileName(resourceName));
try{
if (FileTypeInference.isCodeFileName(resourceName)) return ResourceType.Code;
if (FileTypeInference.isDataFileName(resourceName))
{
return ResourceType.Data;
}
if (FileTypeInference.isModelFileName(resourceName)) return ResourceType.Model;
throw new System.Exception();
}
catch (System.Exception)
{
throw new ArgumentProcessException($"Could not infer resource type from extension of {resourceName}");
}
}
private static void validateVersionString(string resourceStr, string version)
{
string[] versionSections = version.Split('.');
foreach (string section in versionSections)
{
if (section.Trim() != section)
{
throw new ArgumentProcessException("Version cannot contain spaces");
}
// if this is section empty
if (section.Length == 0)
{
// if this is also the only section
if (versionSections.Length == 1)
{
throw new ArgumentProcessException($"\"{resourceStr}\" is missing version");
}
else
{
throw new ArgumentProcessException(
$"Invalid version \"{version}\" in \"{resourceStr}\": Version should be " +
"series of numbers separated by periods, like 1.2.3 or 333.3.20"
);
}
}
foreach (char ch in section)
{
if (!"1234567890".Contains(ch))
{
throw new ArgumentProcessException($"Invalid version character \"{ch}\" in {resourceStr}");
}
}
}
}
public static StreamWriter createOrOverwriteResourceVersionFile(ResourceType resourceType, string resourceName)
{
try
{
string filePath = Path.Join(resourceDirPath(resourceType), nyokaFolderName, resourceName + nyokaVersionExtension);
if (File.Exists(filePath))
{
File.Delete(filePath);
}
return File.CreateText(filePath);
}
catch (System.Exception)
{
throw new Exception($"Failed to create metadata file for {resourceType.ToString().ToLower()} resource {resourceName}");
}
}
}
}