Commits

Kevin Berridge committed 9548b5d

Reorged and renamed

Comments (0)

Files changed (17)

 syntax: glob
 
 *.swp
+Gemfile.lock
+.DS_Store
+source :rubygems
+
+gem "rspec"
+gem "nokogiri"
+require 'rubygems'
+require 'nokogiri'
+
+class Prjfile
+  def initialize xml
+    @doc = Nokogiri::XML( xml )
+  end
+
+  def extract
+    @doc.xpath( '//xmlns:Compile/@Include' ).each do |f|
+      yield f.value
+    end
+  end
+
+  def add str
+    ig = @doc.xpath( '//xmlns:ItemGroup' )[0]
+    compile = Nokogiri::XML::Node.new('Compile', @doc)
+    compile['Include'] = str
+    ig.add_child( compile )
+  end
+
+  def text
+    @doc.to_xml
+  end
+
+  def remove str
+    xp = "//xmlns:Compile[@Include = \"#{str}\"]"
+    n = @doc.xpath( xp )
+    n.remove
+  end
+end

lib/prjfile_finder.rb

+require 'rubygems'
+
+class PrjfileFinder
+  def find_from(file)
+    dir = File.dirname file
+    Dir.chdir dir
+    begin
+      matches = Dir.glob('*.csproj')
+      return build_full_windows_file_path(matches[0]) if matches.length > 0
+
+      lst_dir = Dir.pwd
+      Dir.chdir '..'
+    end while lst_dir != Dir.pwd
+
+    return nil
+  end
+
+  def build_full_windows_file_path(file)
+    File.join(Dir.pwd, file).gsub('/','\\') 
+  end
+end

lib/prjsync.rb

-require_relative 'prjsync/prjfile'
-require_relative 'prjsync/prjfile_finder'

lib/prjsync/prjfile.rb

-require 'rubygems'
-require 'nokogiri'
-
-module Prjsync
-  class Prjfile
-    def initialize xml
-      @doc = Nokogiri::XML( xml )
-    end
-
-    def extract
-      @doc.xpath( '//xmlns:Compile/@Include' ).each do |f|
-        yield f.value
-      end
-    end
-
-    def add str
-      ig = @doc.xpath( '//xmlns:ItemGroup' )[0]
-      compile = Nokogiri::XML::Node.new('Compile', @doc)
-      compile['Include'] = str
-      ig.add_child( compile )
-    end
-
-    def text
-      @doc.to_xml
-    end
-
-    def remove str
-      xp = "//xmlns:Compile[@Include = \"#{str}\"]"
-      n = @doc.xpath( xp )
-      n.remove
-    end
-  end
-end

lib/prjsync/prjfile_finder.rb

-require 'rubygems'
-
-module Prjsync
-  class PrjfileFinder
-    def find_from(file)
-      dir = File.dirname file
-      Dir.chdir dir
-      begin
-        matches = Dir.glob('*.csproj')
-        return build_full_windows_file_path(matches[0]) if matches.length > 0
-
-        lst_dir = Dir.pwd
-        Dir.chdir '..'
-      end while lst_dir != Dir.pwd
-
-      return nil
-    end
-
-    def build_full_windows_file_path(file)
-      File.join(Dir.pwd, file).gsub('/','\\') 
-    end
-  end
-end

prjadder.bat

-@ECHO OFF
-ruby "%~dpn0".rb %*

prjadder.rb

-require_relative './lib/prjsync.rb'
-
-file = ARGV[0]
-finder = Prjsync::PrjfileFinder.new
-prjfile = finder.find_from file
-prj = Prjsync::Prjfile.new( File.open( prjfile, 'r') )
-prj.add(file)
-File.open(prjfile, 'w') { |f| f.puts prj.text }

prjsync/testA/myfile.csproj

+Some file

spec/prjfile_finder_spec.rb

+require_relative '../lib/prjfile_finder'
+
+describe PrjfileFinder do
+  before :all do
+    Dir.mkdir('./prjsync') unless Dir.exists? './prjsync'
+    Dir.mkdir('./prjsync/testA') unless Dir.exists? './prjsync/testA'
+    Dir.mkdir('./prjsync/testA/testB') unless Dir.exists? './prjsync/testA/testB'
+    File.open('./prjsync/testA/myfile.csproj', 'w') { |f| f.puts 'Some file' }
+  end
+
+  it "find file" do
+    finder = PrjfileFinder.new
+    found = finder.find_from './prjsync/testA/testB/fakefile.cs'
+    found.should match /.*\\prjsync\\testA\\myfile.csproj/
+  end
+end 

spec/prjfile_spec.rb

+require_relative '../lib/prjfile'
+require 'tmpdir'
+
+describe Prjfile do
+  str = <<eos
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<ItemGroup>
+  <Compile Include="Folder\\Path\\File.cs" />
+  <Compile Include="Folder\\Other.js" />
+  <Compile Include="More\\Tests.png" />
+  <Compile Include="Hot\\Dogs.dll" />
+</ItemGroup>
+</Project>
+eos
+
+  before :all do
+    # write str to file
+    file = Dir.tmpdir + File::SEPARATOR + "in.xml"
+    File.open( file, 'w' ) { |f| f.write( str ) }
+    @prjFile = Prjfile.new( File.open( file, 'r' ) )
+  end
+
+  describe "extract" do
+    it "extracts all included files" do
+      files = []
+      @prjFile.extract do |f| 
+        files << f
+      end
+
+      files.should == [ 'Folder\Path\File.cs', 'Folder\Other.js', 
+        'More\Tests.png', 'Hot\Dogs.dll' ]
+    end
+  end
+
+  describe "add" do
+    it "adds the file to the project file" do
+      testFile = 'Test\Add\File.cs'
+      @prjFile.add( testFile )
+      
+      doc = Nokogiri::XML( @prjFile.text )
+      val = doc.xpath("//xmlns:Compile[@Include = \"#{testFile}\"]")
+      val.should_not be_nil
+    end
+  end
+
+  describe "remove" do
+    it "removes the file from the project file" do
+      testFile = "Hot\\Dogs.dll"
+      @prjFile.remove( testFile )
+
+      doc = Nokogiri::XML( @prjFile.text )
+      val = doc.xpath( "//xmlns:Compile[@Include = \"#{testFile}\"]")
+      val.length.should == 0
+    end
+  end
+end

spec/prjsync/prjfile_finder_spec.rb

-require 'spec_helper'
-
-module Prjsync
-  describe PrjfileFinder do
-    before :all do
-      Dir.mkdir('C:\\prjsync\\') unless Dir.exists? 'C:\\prjsync'
-      Dir.mkdir('C:\\prjsync\\testA\\') unless Dir.exists? 'C:\\prjsync\\testA'
-      Dir.mkdir('C:\\prjsync\\testA\\testB\\') unless Dir.exists? 'C:\\prjsync\\testA\\testB'
-      File.open('C:\\prjsync\\testA\\myfile.csproj', 'w') { |f| f.puts 'Some file' }
-    end
-
-    it "find file" do
-      finder = PrjfileFinder.new
-      found = finder.find_from 'C:\\prjsync\\testA\\testB\\fakefile.cs'
-      found.should == 'C:\\prjsync\\testA\\myfile.csproj'
-    end
-  end 
-end

spec/prjsync/prjfile_spec.rb

-require 'spec_helper'
-require 'tmpdir'
-
-module Prjsync
-  describe Prjfile do
-    str = <<eos
-<?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <ItemGroup>
-    <Compile Include="Folder\\Path\\File.cs" />
-    <Compile Include="Folder\\Other.js" />
-    <Compile Include="More\\Tests.png" />
-    <Compile Include="Hot\\Dogs.dll" />
-  </ItemGroup>
-</Project>
-eos
-
-    before :all do
-      # write str to file
-      file = Dir.tmpdir + File::SEPARATOR + "in.xml"
-      File.open( file, 'w' ) { |f| f.write( str ) }
-      @prjFile = Prjsync::Prjfile.new( File.open( file, 'r' ) )
-    end
-
-    describe "extract" do
-      it "extracts all included files" do
-        files = []
-        @prjFile.extract do |f| 
-          files << f
-        end
-
-        files.should == [ 'Folder\Path\File.cs', 'Folder\Other.js', 
-          'More\Tests.png', 'Hot\Dogs.dll' ]
-      end
-    end
-
-    describe "add" do
-      it "adds the file to the project file" do
-        testFile = 'Test\Add\File.cs'
-        @prjFile.add( testFile )
-        
-        doc = Nokogiri::XML( @prjFile.text )
-        val = doc.xpath("//xmlns:Compile[@Include = \"#{testFile}\"]")
-        val.should_not be_nil
-      end
-    end
-
-    describe "remove" do
-      it "removes the file from the project file" do
-        testFile = "Hot\\Dogs.dll"
-        @prjFile.remove( testFile )
-
-        doc = Nokogiri::XML( @prjFile.text )
-        val = doc.xpath( "//xmlns:Compile[@Include = \"#{testFile}\"]")
-        val.length.should == 0
-      end
-    end
-  end
-end

spec/spec_helper.rb

-require 'prjsync'
+@ECHO OFF
+ruby "%~dpn0".rb %*
+require_relative './lib/prjfile.rb'
+require_relative './lib/prjfile_finder.rb'
+
+file = ARGV[0]
+finder = PrjfileFinder.new
+prjfile_name = finder.find_from file
+if not prjfile_name
+  exit
+end
+prjfile = Prjfile.new( File.open( prjfile_name, 'r') )
+prjfile.add(file)
+File.open(prjfile_name, 'w') { |f| f.puts prjfile.text }