Initial support for data source creation in Intellij IDEA project files.
authorPeter Donald <donaldp@apache.org>
Mon, 30 Sep 2013 23:14:56 +0000 (23:14 +0000)
committerPeter Donald <donaldp@apache.org>
Mon, 30 Sep 2013 23:14:56 +0000 (23:14 +0000)
git-svn-id: https://svn.apache.org/repos/asf/buildr/trunk@1527821 13f79535-47bb-0310-9956-ffa450edef68

CHANGELOG
lib/buildr/ide/idea.rb
spec/ide/idea_spec.rb

index d67244b..93f51be 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,4 +1,6 @@
 1.4.13 (Pending)
+* Added:  Initial support for data source creation in Intellij IDEA
+          project files.
 * Fixed:  BUILDR-676 - Rework Java::Command:java so that it does not
           reject the :dir option. Reported by John Roth.
 * Added:  Auto-detect jpa provider in Intellij IDEA facet generation if
index 27b923f..7d373b7 100644 (file)
@@ -100,9 +100,9 @@ module Buildr #:nodoc:
         @components ||= []
       end
 
-      def create_composite_component(name, components)
+      def create_composite_component(name, attrs, components)
         return nil if components.empty?
-        component = self.create_component(name)
+        component = self.create_component(name, attrs)
         components.each do |element|
           element = element.call if element.is_a?(Proc)
           component.add_element element
@@ -477,7 +477,7 @@ module Buildr #:nodoc:
       end
 
       def facet_component
-        create_composite_component("FacetManager", self.facets)
+        create_composite_component("FacetManager", {}, self.facets)
       end
 
       def module_root_component
@@ -615,6 +615,7 @@ module Buildr #:nodoc:
     class IdeaProject < IdeaFile
       attr_accessor :extra_modules
       attr_accessor :artifacts
+      attr_accessor :data_sources
       attr_accessor :configurations
       attr_writer :jdk_version
       attr_writer :version
@@ -624,6 +625,7 @@ module Buildr #:nodoc:
         @buildr_project = buildr_project
         @extra_modules = []
         @artifacts = []
+        @data_sources = []
         @configurations = []
       end
 
@@ -651,6 +653,48 @@ module Buildr #:nodoc:
         end
       end
 
+      def add_postgres_data_source(name, options = {})
+
+        if options[:url].nil? && options[:database]
+         default_url = "jdbc:postgresql://#{(options[:host] || "127.0.0.1")}:#{(options[:port] || "5432")}/#{options[:database]}"
+        end
+
+        params = {
+          :driver => 'org.postgresql.Driver',
+          :url => default_url,
+          :username => ENV["USER"],
+          :classpath => ["org.postgresql:postgresql:jar:9.2-1003-jdbc4"]
+        }.merge(options)
+        add_data_source(name, params)
+      end
+
+      def add_data_source(name, options = {})
+        add_to_composite_component(self.data_sources) do |xml|
+          data_source_options = {
+            :source => "LOCAL",
+            :name => name,
+            :uuid => Buildr::Util.uuid
+          }
+          classpath = options[:classpath] || []
+          xml.tag!("data-source", data_source_options) do |xml|
+            xml.tag!("synchronize", (options[:synchronize]||"true"))
+            xml.tag!("jdbc-driver", options[:driver]) if options[:driver]
+            xml.tag!("jdbc-url", options[:url]) if options[:url]
+            xml.tag!("user-name", options[:username]) if options[:username]
+            xml.tag!("user-password", encrypt(options[:password])) if options[:password]
+            xml.libraries do |xml|
+              classpath.each do |classpath_element|
+                a = Buildr.artifact(classpath_element)
+                a.invoke
+                xml.library do |xml|
+                  xml.tag!("url", resolve_path(a.to_s))
+                end
+              end
+            end if classpath.size > 0
+          end
+        end
+      end
+
       def add_exploded_war_artifact(project, options = {})
         artifact_name = options[:name] || project.iml.id
         build_on_make = options[:build_on_make].nil? ? false : options[:build_on_make]
@@ -815,6 +859,7 @@ module Buildr #:nodoc:
           lambda { modules_component },
           vcs_component,
           artifacts_component,
+          lambda { data_sources_component },
           configurations_component,
           lambda { framework_detection_exclusion_component }
         ]
@@ -907,12 +952,16 @@ module Buildr #:nodoc:
         end
       end
 
+      def data_sources_component
+        create_composite_component("DataSourceManagerImpl", {:format => "xml", :hash => "3208837817"}, self.data_sources)
+      end
+
       def artifacts_component
-        create_composite_component("ArtifactManager", self.artifacts)
+        create_composite_component("ArtifactManager", {}, self.artifacts)
       end
 
       def configurations_component
-        create_composite_component("ProjectRunConfigurationManager", self.configurations)
+        create_composite_component("ProjectRunConfigurationManager", {}, self.configurations)
       end
 
       def resolve_path(path)
@@ -921,6 +970,10 @@ module Buildr #:nodoc:
 
     private
 
+      def encrypt(password)
+        password.bytes.inject("") { |x, y| x + (y ^ 0xdfaa).to_s(16) }
+      end
+
       def partition_dependencies(dependencies)
         libraries = []
         projects = []
index 744ac68..e724a73 100644 (file)
@@ -729,6 +729,59 @@ describe Buildr::IntellijIdea do
       end\r
     end\r
 \r
+    describe "with add_data_source" do\r
+      before do\r
+        artifact("org.postgresql:postgresql:jar:9.not-a-version") { |task| write task.name }\r
+        @foo = define "foo" do\r
+          ipr.add_data_source("Postgres",\r
+                              :driver => 'org.postgresql.Driver',\r
+                              :url => "jdbc:postgresql://127.0.0.1:5432/MyDb",\r
+                              :username => "MyDBUser",\r
+                              :password => "secreto",\r
+                              :classpath => ["org.postgresql:postgresql:jar:9.not-a-version"])\r
+        end\r
+        invoke_generate_task\r
+      end\r
+\r
+      it "generates a data source manager with specified data source" do\r
+        doc = xml_document(@foo._("foo.ipr"))\r
+        prefix_xpath = "/project/component[@name='DataSourceManagerImpl', @format='xml', @hash='3208837817']/data-source"\r
+        doc.should have_nodes(prefix_xpath, 1)\r
+        ds_path = "#{prefix_xpath}[@source='LOCAL', @name='Postgres']"\r
+        doc.should have_xpath(ds_path)\r
+        doc.should have_xpath("#{ds_path}/synchronize/text() = 'true'")\r
+        doc.should have_xpath("#{ds_path}/jdbc-driver/text() = 'org.postgresql.Driver'")\r
+        doc.should have_xpath("#{ds_path}/jdbc-url/text() = 'jdbc:postgresql://127.0.0.1:5432/MyDb'")\r
+        doc.should have_xpath("#{ds_path}/user-name/text() = 'MyDBUser'")\r
+        doc.should have_xpath("#{ds_path}/user-password/text() = 'dfd9dfcfdfc9dfd8dfcfdfdedfc5'")\r
+        doc.should have_xpath("#{ds_path}/libraries/library/url/text() = '$MAVEN_REPOSITORY$/org/postgresql/postgresql/9.not-a-version/postgresql-9.not-a-version.jar'")\r
+      end\r
+    end\r
+\r
+    describe "with add_postgres_data_source" do\r
+      before do\r
+        ENV["USER"] = "Bob"\r
+        artifact("org.postgresql:postgresql:jar:9.2-1003-jdbc4") { |task| write task.name }\r
+        @foo = define "foo" do\r
+          ipr.add_postgres_data_source("Postgres", :database => 'MyDb')\r
+        end\r
+        invoke_generate_task\r
+      end\r
+\r
+      it "generates a data source manager with specified data source" do\r
+        doc = xml_document(@foo._("foo.ipr"))\r
+        prefix_xpath = "/project/component[@name='DataSourceManagerImpl', @format='xml', @hash='3208837817']/data-source"\r
+        doc.should have_nodes(prefix_xpath, 1)\r
+        ds_path = "#{prefix_xpath}[@source='LOCAL', @name='Postgres']"\r
+        doc.should have_xpath(ds_path)\r
+        doc.should have_xpath("#{ds_path}/synchronize/text() = 'true'")\r
+        doc.should have_xpath("#{ds_path}/jdbc-driver/text() = 'org.postgresql.Driver'")\r
+        doc.should have_xpath("#{ds_path}/jdbc-url/text() = 'jdbc:postgresql://127.0.0.1:5432/MyDb'")\r
+        doc.should have_xpath("#{ds_path}/user-name/text() = 'Bob'")\r
+        doc.should have_xpath("#{ds_path}/libraries/library/url/text() = '$MAVEN_REPOSITORY$/org/postgresql/postgresql/9.2-1003-jdbc4/postgresql-9.2-1003-jdbc4.jar'")\r
+      end\r
+    end\r
+\r
     describe "with artifacts added to root project" do\r
       before do\r
         @foo = define "foo" do\r