rakelib/support.rb
changeset 289 c10f5a5b2e92
parent 288 2986b947d89f
child 322 d31ea885c8fa
--- a/rakelib/support.rb	Fri Jan 17 10:36:16 2020 +0000
+++ b/rakelib/support.rb	Fri Jan 17 21:37:47 2020 +0000
@@ -137,4 +137,74 @@
       end
     end # class ArtifactRepository::Jenkins::Build
   end # class ArtifactRepository::Jenkins
+
+  class PlainHTTPListing
+
+    def initialize(uri)
+      @uri = uri
+      @html_data = Net::HTTP.get(URI(uri))
+    end
+
+    def latestBuild()
+      return Build.new(@uri + latest_build + '/') # dont forget trailing '/'!
+    end
+
+    class Build
+      attr_reader :uri, :html_data
+
+      def initialize(uri)
+        @uri = uri
+        @html_data = Net::HTTP.get(URI(uri))
+      end
+
+      def artifacts
+        unless @artifacts
+          # get latest build file list
+          @artifacts = lastest_build_files(Array.new, String.new).collect {|each_file| Artifact.new(each_file, URI(@uri + each_file))} 
+        end
+        @artifacts
+      end
+
+      private
+
+      # parse HTML via regexp (yes, bad idea but Apache can't serve natively JSON)
+      # retuns Array of String(s) - filenames and their suffixes
+      def parse_html_directories(html_build_directory_list)
+        files_and_suffixes = html_build_directory_list.scan(/(smalltalkx-jv-branch-\d+.\d+.\d+_build\d+[a-zA-Z0-9_\-]+\.|tar\.bz2|zip|7z|7zip|\.sha256|\.sha512)/)
+        files_and_suffixes.flatten
+      end
+
+      # create file array where the file part and its suffix will be merged together
+      # returns Array of String filenames without any duplicities (needed due to HTML nature (a href))
+      def lastest_build_files(stx_latest_build_files, stx_file)  
+        parse_html_directories(@html_data).each do |file_part|
+          if file_part.match(/\.$/) # only a file_part has a . at the end
+            stx_latest_build_files.push(stx_file) unless stx_file.empty? 
+            stx_file = String.new  # empty current String content
+            stx_file = file_part
+          else 
+            stx_file = stx_file + file_part
+          end
+        end
+        # prune (duplicities)
+        stx_latest_build_files.uniq!
+      end
+
+    end # class ArtifactRepository::PlainHTTPListing::Build
+
+    private
+
+    # find newest build contains date: yyyy-mm-dd_buildId
+    def latest_build
+      directory_list.max
+    end
+
+    # get Array of directories
+    def directory_list
+      directories=@html_data.scan(/\d{4}-\d{2}-\d{2}_\d+/)
+      directories.uniq! # remove duplicates
+    end
+
+  end # class ArtifactRepository::PlainHTTPListing
+
 end # module ArtifactRepository
\ No newline at end of file