You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by do...@apache.org on 2013/10/01 01:14:57 UTC

svn commit: r1527821 - in /buildr/trunk: CHANGELOG lib/buildr/ide/idea.rb spec/ide/idea_spec.rb

Author: donaldp
Date: Mon Sep 30 23:14:56 2013
New Revision: 1527821

URL: http://svn.apache.org/r1527821
Log:
Initial support for data source creation in Intellij IDEA project files.

Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/lib/buildr/ide/idea.rb
    buildr/trunk/spec/ide/idea_spec.rb

Modified: buildr/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=1527821&r1=1527820&r2=1527821&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Mon Sep 30 23:14:56 2013
@@ -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

Modified: buildr/trunk/lib/buildr/ide/idea.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/ide/idea.rb?rev=1527821&r1=1527820&r2=1527821&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/ide/idea.rb (original)
+++ buildr/trunk/lib/buildr/ide/idea.rb Mon Sep 30 23:14:56 2013
@@ -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 = []

Modified: buildr/trunk/spec/ide/idea_spec.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/ide/idea_spec.rb?rev=1527821&r1=1527820&r2=1527821&view=diff
==============================================================================
--- buildr/trunk/spec/ide/idea_spec.rb (original)
+++ buildr/trunk/spec/ide/idea_spec.rb Mon Sep 30 23:14:56 2013
@@ -729,6 +729,59 @@ describe Buildr::IntellijIdea do
       end
     end
 
+    describe "with add_data_source" do
+      before do
+        artifact("org.postgresql:postgresql:jar:9.not-a-version") { |task| write task.name }
+        @foo = define "foo" do
+          ipr.add_data_source("Postgres",
+                              :driver => 'org.postgresql.Driver',
+                              :url => "jdbc:postgresql://127.0.0.1:5432/MyDb",
+                              :username => "MyDBUser",
+                              :password => "secreto",
+                              :classpath => ["org.postgresql:postgresql:jar:9.not-a-version"])
+        end
+        invoke_generate_task
+      end
+
+      it "generates a data source manager with specified data source" do
+        doc = xml_document(@foo._("foo.ipr"))
+        prefix_xpath = "/project/component[@name='DataSourceManagerImpl', @format='xml', @hash='3208837817']/data-source"
+        doc.should have_nodes(prefix_xpath, 1)
+        ds_path = "#{prefix_xpath}[@source='LOCAL', @name='Postgres']"
+        doc.should have_xpath(ds_path)
+        doc.should have_xpath("#{ds_path}/synchronize/text() = 'true'")
+        doc.should have_xpath("#{ds_path}/jdbc-driver/text() = 'org.postgresql.Driver'")
+        doc.should have_xpath("#{ds_path}/jdbc-url/text() = 'jdbc:postgresql://127.0.0.1:5432/MyDb'")
+        doc.should have_xpath("#{ds_path}/user-name/text() = 'MyDBUser'")
+        doc.should have_xpath("#{ds_path}/user-password/text() = 'dfd9dfcfdfc9dfd8dfcfdfdedfc5'")
+        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'")
+      end
+    end
+
+    describe "with add_postgres_data_source" do
+      before do
+        ENV["USER"] = "Bob"
+        artifact("org.postgresql:postgresql:jar:9.2-1003-jdbc4") { |task| write task.name }
+        @foo = define "foo" do
+          ipr.add_postgres_data_source("Postgres", :database => 'MyDb')
+        end
+        invoke_generate_task
+      end
+
+      it "generates a data source manager with specified data source" do
+        doc = xml_document(@foo._("foo.ipr"))
+        prefix_xpath = "/project/component[@name='DataSourceManagerImpl', @format='xml', @hash='3208837817']/data-source"
+        doc.should have_nodes(prefix_xpath, 1)
+        ds_path = "#{prefix_xpath}[@source='LOCAL', @name='Postgres']"
+        doc.should have_xpath(ds_path)
+        doc.should have_xpath("#{ds_path}/synchronize/text() = 'true'")
+        doc.should have_xpath("#{ds_path}/jdbc-driver/text() = 'org.postgresql.Driver'")
+        doc.should have_xpath("#{ds_path}/jdbc-url/text() = 'jdbc:postgresql://127.0.0.1:5432/MyDb'")
+        doc.should have_xpath("#{ds_path}/user-name/text() = 'Bob'")
+        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'")
+      end
+    end
+
     describe "with artifacts added to root project" do
       before do
         @foo = define "foo" do