Skip to content

Commit 8d875c7

Browse files
author
Rustam
committed
Many enhancemenets
1. Bux fixes 2. Supports downloading image files(possibly any files if specified) 3. Supports branch selection
1 parent 523b4b6 commit 8d875c7

File tree

1 file changed

+160
-82
lines changed

1 file changed

+160
-82
lines changed

CacheUpdater/Task.cls.xml

Lines changed: 160 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<Export generator="Cache" version="25" zv="Cache for Windows (x86-64) 2016.2 (Build 721U)" ts="2016-09-04 22:50:49">
2+
<Export generator="Cache" version="25" zv="Cache for Windows (x86-64) 2016.2 (Build 721U)" ts="2016-09-12 18:18:41">
33
<Class name="CacheUpdater.Task">
4-
<Super>%SYS.Task.Definition</Super>
5-
<TimeChanged>64161,48985.224853</TimeChanged>
4+
<Super>%SYS.Task.Definition,CacheUpdater.UDL</Super>
5+
<TimeChanged>64173,65712.36031</TimeChanged>
66
<TimeCreated>63603,52252.541311</TimeCreated>
77

88
<Parameter name="TaskName">
@@ -75,20 +75,21 @@ Unauthenticated requests are associated with an IP address.<br>
7575
For example in the repository: https://github.com/intersystems-ru/Cache-MDX2JSON<br>
7676
Owner - intersystems-ru, Repository - Cache-MDX2JSON.<br> ]]></Description>
7777
<ClassMethod>1</ClassMethod>
78-
<FormalSpec>Owner:%String,Repository:%String,Branch:%String="",Username:%String,Password:%String,Namespace=$Namespace</FormalSpec>
78+
<FormalSpec>Owner:%String,Repository:%String,Branch:%String="",Username:%String="",Password:%String="",Namespace=$Namespace</FormalSpec>
7979
<ReturnType>%Status</ReturnType>
8080
<Implementation><![CDATA[
81+
#dim req As %Net.HttpRequest
8182
Set req = ..CreateRequest(Username, Password)
8283
Set req.Location = "repos/" _ Owner _ "/" _ Repository _ "/contents" // as described in https://developer.github.com/v3/repos/
83-
Do:(Branch'="") req.SetParam("ref",Branch) // if omitted the repository’s default branch (usually master) would be used
8484
8585
Set links = ##class(%ListOfDataTypes).%New()
86-
Set st = ..ProcessDirectory("",req,.links)
86+
Set st = ..ProcessDirectory("",.req,Branch,.links)
8787
Return:$$$ISERR(st) st
8888
8989
Set namespace = $Namespace
9090
Zn Namespace
9191
Set st = ..DownloadFiles(links,req,.list)
92+
zw list
9293
Set st2 = $system.OBJ.CompileList(.list,"cuk /checkuptodate=expandedonly")
9394
Zn namespace
9495
@@ -103,13 +104,15 @@ Process one directory of GitHub repository. Recursive.<br>
103104
<b>Request</b> - Authenticated/Set %Net.HttpRequest object.<br>
104105
<b>Links</b> - List of links to raw files (which satisfy <b>IsCacheFile</b> conditions) from repository.<br>]]></Description>
105106
<ClassMethod>1</ClassMethod>
106-
<FormalSpec><![CDATA[Path:%String="",Request:%Net.HttpRequest,&Links:%ListOfDataTypes]]></FormalSpec>
107+
<FormalSpec><![CDATA[Path:%String="",Request:%Net.HttpRequest,Branch:%String="",&Links:%ListOfDataTypes]]></FormalSpec>
107108
<ReturnType>%Status</ReturnType>
108109
<Implementation><![CDATA[
109110
Set location = Request.Location
110111
Set Request.Location = Request.Location _ Path
112+
Do:(Branch'="") Request.SetParam("ref",Branch)
111113
112114
Set st = Request.Get()
115+
113116
Return:$$$ISERR(st) st
114117
Return:(Request.HttpResponse.StatusCode = 404) $$$ERROR($$$GeneralError,"Repository doesn't exist OR you don't have access")
115118
Return:((Request.HttpResponse.StatusCode = 403) && (Request.HttpResponse.GetHeader("X-RATELIMIT-REMAINING")=0)) $$$ERROR($$$GeneralError,"API rate limit exceeded. Try logging in.")
@@ -123,10 +126,11 @@ Process one directory of GitHub repository. Recursive.<br>
123126
For i = 1:1:objects.Count() {
124127
Set obj = objects.GetAt(i)
125128
If (obj.type = "dir") {
126-
Set st = ..ProcessDirectory("/"_obj.name,Request,.Links)
129+
Set st = ..ProcessDirectory("/"_obj.name,Request,Branch,.Links)
127130
Return:$$$ISERR(st) st
128131
} ElseIf (obj.type = "file") {
129-
Do:..IsCacheFile(obj) Links.Insert(obj."download_url")
132+
//Do:..IsCacheFile(obj) Links.Insert(obj."download_url")
133+
Do Links.Insert($LB(obj."download_url",..IsCacheFile(obj)))
130134
} Else {
131135
// obj.type = "symlink" or obj.type = "submodule"
132136
}
@@ -143,7 +147,7 @@ Check that incoming file is the one you need.</Description>
143147
<FormalSpec>File:%ZEN.proxyObject</FormalSpec>
144148
<ReturnType>%Boolean</ReturnType>
145149
<Implementation><![CDATA[
146-
Set extensions = ",xml,cls,csp,csr,mac,int,bas,inc,gbl,prj,obj,pkg,gof,dfi,pivot,dashboard,html,css,js,ts,scss"
150+
Set extensions = ",xml,cls,csp,csr,mac,int,bas,inc,gbl,prj,obj,pkg,gof,dfi,pivot,dashboard,html,css,js,ts,scss,"
147151
Return:($L(File.name,".")=1) 0 //no extension
148152
Set File.Extension = $P(File.name,".",$L(File.name,"."))
149153
Return $F(extensions,","_$ZCVT(File.Extension,"l")_",")
@@ -163,36 +167,100 @@ Download list of files on https://raw.githubusercontent.com/ server.<br>
163167
Kill Items
164168
Set Request.Server = "raw.githubusercontent.com"
165169
Set st = $$$OK
166-
167-
For i = 1:1:Links.Count() {
168-
Set link = Links.GetAt(i)
169-
Set streq = Request.Get($e(link,35,*)) // Remove "https://raw.githubusercontent.com/" from URL.
170-
Set:$$$ISERR(streq) st=$$$ADDSC(st, streq)
171-
172-
Set binarystream = Request.HttpResponse.Data
173-
Do binarystream.Rewind() // just in case
174-
175-
Set stream=##class(%GlobalCharacterStream).%New() //translating binary stream into character stream
176-
While 'binarystream.AtEnd {
177-
Do stream.WriteLine(binarystream.ReadLine())
178-
}
179-
Do stream.Rewind()
180-
181-
182-
Set strload = ""
183-
If ##class(CacheUpdater.UDL).IsUDLFile(stream) {
184-
Set stload = ##class(CacheUpdater.UDL).LoadUDLFile(stream, link, .items)
185-
}
186-
Else {
187-
Set stload = $system.OBJ.LoadStream(stream,"",.error,.items,,,,"UTF8")
170+
Try
171+
{
172+
For i = 1:1:Links.Count()
173+
{
174+
Set link = $ListGet(Links.GetAt(i),1)
175+
Set bIsCacheFile = $ListGet(Links.GetAt(i),2)
176+
Set ^gitfiles(i,"link")=link
177+
Set ^gitfiles(i,"bIsCacheFile")=bIsCacheFile
178+
179+
Set streq = Request.Get($e(link,35,*)) // Remove "https://raw.githubusercontent.com/" from URL.
180+
If $$$ISERR(streq)
181+
{
182+
Set st=$$$ADDSC(st, streq)
183+
Set ^gitfiles(i,"streq")=streq
184+
Continue
185+
}
186+
187+
Set ^gitfiles(i,"stream")="starting..."
188+
Set binarystream = Request.HttpResponse.Data
189+
190+
Do binarystream.Rewind() // just in case
191+
192+
Set characterStream=##class(%GlobalCharacterStream).%New() //translating binary stream into character stream
193+
Set stTranslate=$$$OK
194+
Try
195+
{
196+
While 'binarystream.AtEnd
197+
{
198+
//Use eol to prevent breaking lines larger than 32Kb
199+
Set line=binarystream.ReadLine(, .stTranslate, .eol)
200+
Quit:$System.Status.IsError(stTranslate)
201+
202+
If eol
203+
{
204+
Set stTranslate=characterStream.WriteLine(line)
205+
}
206+
Else
207+
{
208+
Set stTranslate=characterStream.Write(line)
209+
}
210+
Quit:$System.Status.IsError(stTranslate)
211+
}
212+
Quit:$System.Status.IsError(stTranslate)
213+
214+
Do characterStream.Rewind()
215+
}
216+
Catch (oTranslateStreamException)
217+
{
218+
Set stTranslate=oTranslateStreamException.AsStatus()
219+
}
220+
221+
If $System.Status.IsError(stTranslate)
222+
{
223+
//Could not convert binary stream to character stream
224+
//It is probably a binary file anyway
225+
Set characterStream=""
226+
Set st=$$$ADDSC(st, stTranslate)
227+
Set ^gitfiles(i,"stTranslate")=stTranslate
228+
}
229+
Set ^gitfiles(i,"stream")="Done"
230+
231+
Do binarystream.Rewind()
232+
233+
Set stload = $$$OK
234+
235+
set items = ""
236+
If ('$IsObject(characterStream)) || (..IsUDLFile(characterStream))
237+
{
238+
Set ^gitfiles(i,"IsUDLFile")="1"
239+
Set stload = ..LoadUDLFile(characterStream, binarystream, link, .items)
240+
}
241+
ElseIf bIsCacheFile
242+
{
243+
Set ^gitfiles(i,"IsUDLFile")="0"
244+
Set stload = $system.OBJ.LoadStream(characterStream,"",.error,.items,,,,"UTF8")
245+
}
246+
Set ^gitfiles(i,"stload")=stload
247+
If $$$ISERR(stload)
248+
{
249+
Set st=$$$ADDSC(st, stload)
250+
Continue
251+
}
252+
Merge Items = items // Does not overwrite existing array keys: Items(itemname)=""
188253
}
189254
190-
Set:$$$ISERR(stload) st=$$$ADDSC(st, stload)
191-
Merge Items = items // Does not overwrite existing array keys: Items(itemname)=""
255+
Set Request.Server="api.github.com"
256+
}
257+
Catch (oException)
258+
{
259+
Set st = oException.AsStatus()
260+
If $D(i) Set ^gitfiles(i,"st final")=st
192261
}
193262
194-
Set Request.Server="api.github.com"
195-
Return st
263+
Quit st
196264
]]></Implementation>
197265
</Method>
198266

@@ -227,7 +295,7 @@ Download list of files on https://raw.githubusercontent.com/ server.<br>
227295

228296
<Class name="CacheUpdater.UDL">
229297
<Super>%RegisteredObject</Super>
230-
<TimeChanged>64165,82238.47126</TimeChanged>
298+
<TimeChanged>64173,65465.415142</TimeChanged>
231299
<TimeCreated>64161,48850.325593</TimeCreated>
232300

233301
<Method name="IsUDLFile">
@@ -265,23 +333,8 @@ Finds the first string in a line
265333
<FormalSpec>line:%String</FormalSpec>
266334
<ReturnType>%String</ReturnType>
267335
<Implementation><![CDATA[
268-
set counter = 1
269-
set letter = $extract(line, counter)
270-
while letter '= "" {
271-
if letter = " " {
272-
set counter = counter + 1
273-
set letter = $extract(line, counter)
274-
continue
275-
} else {
276-
set ext = $extract(line, counter, *)
277-
set index = $find(ext, " ")
278-
if index = 0 {
279-
set index = 9999999999
280-
}
281-
return $extract(ext, 0, index - 2)
282-
}
283-
}
284-
return ""
336+
set trimmed = $zstrip(line, "<>W")
337+
return $piece(trimmed, " ")
285338
]]></Implementation>
286339
</Method>
287340

@@ -396,8 +449,9 @@ Get extension of the file by url
396449
<FormalSpec>url:%String</FormalSpec>
397450
<ReturnType>%String</ReturnType>
398451
<Implementation><![CDATA[
399-
400-
return $zconvert($piece(url, ".", *), "l")
452+
//return $zconvert($piece(url, ".", *), "l")
453+
//AMIR: There are parameters after the extension that are not part of the extension
454+
return $zconvert($piece($piece(url, ".", *),"?"), "l")
401455
]]></Implementation>
402456
</Method>
403457

@@ -409,7 +463,6 @@ Check whether a file is a web file
409463
<FormalSpec>ext:%String</FormalSpec>
410464
<ReturnType>%String</ReturnType>
411465
<Implementation><![CDATA[
412-
413466
set webExts = "csp,html,css,js,ts,scss"
414467
return $find(webExts, ext)
415468
]]></Implementation>
@@ -422,12 +475,13 @@ Imports the file in UDL file in the project
422475
<b>url</b> - the url where the file is located in the web.<br>
423476
<b>list</b> - array of files to compile<br>]]></Description>
424477
<ClassMethod>1</ClassMethod>
425-
<FormalSpec>contentStream:%GlobalCharacterStream,url:%String,list:%String</FormalSpec>
478+
<FormalSpec>contentStream:%GlobalCharacterStream,binaryStream:%Stream.FileCharacterGzip,url:%String,list:%String</FormalSpec>
426479
<ReturnType>%Status</ReturnType>
427480
<Implementation><![CDATA[
428481
set st = $$$OK
429482
430483
set ext = ..GetExt(url)
484+
431485
if ext = "cls" {
432486
set st = ..CreateClass(contentStream, url, .list)
433487
}
@@ -436,10 +490,11 @@ Imports the file in UDL file in the project
436490
}
437491
elseif (ext = "inc") || (ext = "mac") {
438492
set st = ..CreateRoutine(contentStream, url, .list)
439-
} elseif ..IsWebFile(ext) {
440-
set st = ..CreateWebFile(contentStream, url, ext, .list)
493+
}
494+
else
495+
{
496+
set st = ..CreateWebFile(contentStream, binaryStream, url, ext, .list)
441497
}
442-
443498
return st
444499
]]></Implementation>
445500
</Method>
@@ -585,34 +640,57 @@ Creates and imports mac, int, inc files into the project from stream
585640
<b>ext</b> - extension of the file<br>
586641
<b>list</b> - array of files to compile<br>]]></Description>
587642
<ClassMethod>1</ClassMethod>
588-
<FormalSpec><![CDATA[contentStream:%GlobalCharacterStream,url:%String,ext:%String,&list:%String]]></FormalSpec>
643+
<FormalSpec><![CDATA[contentStream:%GlobalCharacterStream,binaryStream:%Stream.FileCharacterGzip,url:%String,ext:%String,&list:%String]]></FormalSpec>
589644
<ReturnType>%Status</ReturnType>
590645
<Implementation><![CDATA[
591646
Set st = $$$OK
592-
593-
Set filestream = ##class(%Stream.FileCharacter).%New()
594-
Do contentStream.Rewind()
595-
596-
set CSPPath = $system.CSP.GetFileName($system.CSP.GetDefaultApp($namespace)_"/")
597-
set fileDirectory = $translate($Piece(url, "csp/", 2), "\", "/")
598-
set dirChain = $p(CSPPath_fileDirectory,$p($translate(CSPPath_fileDirectory,"\","/"),"/",*),1)
599-
set dirChain = $translate(dirChain, "\", "/")
600-
do ##class(%File).CreateDirectoryChain(dirChain)
601-
set st = filestream.LinkToFile(CSPPath_fileDirectory)
602-
603-
While 'contentStream.AtEnd
647+
Try
604648
{
605-
Do filestream.WriteLine(contentStream.ReadLine())
649+
Set tDefaultApp=$system.CSP.GetDefaultApp($namespace)_"/"
650+
set tCSPRootPath = $system.CSP.GetFileName(tDefaultApp)
651+
652+
Set tFileName = $Piece($Piece(url,"?",1),"/",*)
653+
654+
Set tCSPSubPath = $Piece(url,tDefaultApp,2,*) //still has filename and ? parameters in it
655+
Set tCSPSubPath = $Piece(tCSPSubPath, tFileName,1) //now it does't
656+
657+
set tFileDirectory = tCSPRootPath_tCSPSubPath
658+
Set tFullFileName = tFileDirectory_tFileName
659+
660+
//On Windows, tFullFileName will contain \ and / but CreateDirectoryChain() and
661+
//LinkToFile() already normalize the paths accordingly to the OS for us so
662+
//we don't have to worry about it.
663+
If '##class(%File).CreateDirectoryChain(tFileDirectory)
664+
{
665+
Set st = $System.Status.Error(5001,"Could nor create path chain '"_tFileDirectory_"'")
666+
Quit
667+
}
668+
669+
Set filestream = ##class(%Stream.FileCharacter).%New()
670+
set st = filestream.LinkToFile(tFullFileName)
671+
Quit:$System.Status.IsError(st)
672+
673+
If $IsObject(contentStream) && ..IsWebFile(ext)
674+
{
675+
Set st=filestream.CopyFrom(contentStream)
676+
}
677+
Else
678+
{
679+
Set st=filestream.CopyFrom(binaryStream)
680+
}
681+
Quit:$System.Status.IsError(st)
682+
683+
set st = filestream.%Save()
684+
Quit:$System.Status.IsError(st)
685+
686+
Write !, "Imported " _ tFullFileName, !
606687
}
607-
if $$$ISERR(st) Quit st
608-
609-
set st = filestream.%Save()
610-
611-
if st {
612-
w !, "Imported " _ fileDirectory, !
688+
Catch (oException)
689+
{
690+
Set st = oException.AsStatus()
613691
}
614692
615-
Return st
693+
Quit st
616694
]]></Implementation>
617695
</Method>
618696
</Class>

0 commit comments

Comments
 (0)