1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env ruby
#
# Script for quick opening non-NetBeans Ruby project in NetBeans.
#
# Usage: nb_ruby_project <ruby_project_dir> [--rails]
#
# See the blog entry for more:
#   http://blogs.sun.com/martink/entry/open_simply_whatever_non_netbeans
#

# ========== User configuration ============
NB_BIN = '/path/to/netbeans/bin/netbeans'
# NB_USER_DIR = '/path/to/optional/userdir'
# ==========================================

is_rails = ARGV.delete('--rails')

if ARGV.empty?
  puts "Usage: #{File.basename(__FILE__)} <ruby_project_dir> [--rails]"
  exit 1
end

prj_dir = File.expand_path(ARGV[0])
raise "#{prj_dir} must be a valid directory" unless File.directory?(prj_dir)
prj_name = File.basename(prj_dir)

# ========== NetBeans metadata start ==========
prj_xml = if is_rails
  <<DOC
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
    <type>org.netbeans.modules.ruby.railsprojects</type>
    <configuration>
        <data xmlns="http://www.netbeans.org/ns/rails-project/1">
            <name>#{prj_name}</name>
        </data>
    </configuration>
</project>
DOC
  else
  <<DOC
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
    <type>org.netbeans.modules.ruby.rubyproject</type>
    <configuration>
        <data xmlns="http://www.netbeans.org/ns/ruby-project/1">
            <name>#{prj_name}</name>
            <source-roots>
                <root id="src.dir"/>
            </source-roots>
            <test-roots>
                <root id="test.src.dir"/>
                <root id="spec.src.dir"/>
            </test-roots>
        </data>
    </configuration>
</project>
DOC
end

prj_props = <<DOC
platform.active=default
source.encoding=UTF-8
spec.src.dir=spec
DOC

unless is_rails
  prj_props << <<DOC
src.dir=lib
test.src.dir=test
DOC
end
# ========== NetBeans metadata end ==========

nbproject_path = File.join(prj_dir, 'nbproject')
puts "\nProject: #{prj_name} (#{is_rails ? 'Rails' : 'non-Rails'})\n\n"
if File.directory?(nbproject_path)
  puts "NetBeans project detected in: #{prj_dir}."
  puts "  (Skipping NetBeans data initialization)"
else
  full_path = lambda {|f| File.expand_path(File.join(nbproject_path, f))}
  puts "Will create following files (NetBeans metadata) in:"
  puts %w(project.xml project.properties).map{|f| "  #{full_path.call(f)}"}
  print "\nPress \"y<Enter>\" to agree: "
  exit 2 unless $stdin.gets.chomp == 'y'

  Dir.mkdir(nbproject_path)
  dump = lambda do |f, content|
    File.open(File.join(nbproject_path, f), 'w') { |f| f.write(content) }
  end
  dump.call('project.xml', prj_xml)
  dump.call('project.properties', prj_props)

  puts 'Mentioned NetBeans metadata created'
end

# Open file in NetBeans, preferring Rakefile
Dir.chdir(prj_dir) do |path|
  file = 'Rakefile' # prefer Rakefile
  file = Dir.new(path).find{|f| File.file?(f)} unless File.file?(file)
  if file
    puts "\nOpening '#{File.basename(file)}' in NetBeans..."
    user_dir_token = defined?(NB_USER_DIR) ? "--userdir '#{NB_USER_DIR}'" : ''
    system("#{NB_BIN} #{user_dir_token} --open '#{File.expand_path(file)}'")
  end
end