Top Level Namespace

Defined Under Namespace

Modules: Inplace Classes: FileFilter, String, Tempfile

Constant Summary collapse

MYNAME =
File.basename($0)

Instance Method Summary collapse

Instance Method Details

#interruptObject



537
538
539
540
# File 'lib/inplace.rb', line 537

def interrupt
  STDERR.puts "Interrupted."
  exit 130
end

#main(argv) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inplace.rb', line 45

def main(argv)
  $uninterruptible = $interrupt = false

  [:SIGINT, :SIGQUIT, :SIGTERM].each { |sig|
    trap(sig) {
      if $uninterruptible
        $interrupt = true
      else
        interrupt
      end
    }
  }

  usage = "usage: \#{MYNAME} [-Lfinstvz] [-b SUFFIX] COMMANDLINE [file ...]\n\#{MYNAME} [-Lfinstvz] [-b SUFFIX] [-e COMMANDLINE] [file ...]\n"

  banner = "\#{MYNAME} version \#{Inplace::VERSION}\n\nEdits files in-place through given filter commands.\n\n\#{usage}\n"

  filters = []

  $config = Inplace::Config.new
  file = File.expand_path("~/.inplace")
  $config.load(file) if File.exist?(file)

  opts = OptionParser.new(banner, 24) { |opts|
    nextline = "\n" << opts.summary_indent << " " * opts.summary_width << " "

    opts.on("-h", "--help",
      "Show this message.") {
      print opts
      exit 0
    }

    opts.on("-L", "--dereference",
      "Edit the original file for each symlink.") {
      |b| $dereference = b
    }

    opts.on("-b", "--backup-suffix=SUFFIX",
      "Create a backup file with the SUFFIX for each file." << nextline <<
      "Backup files will be written over existing files," << nextline <<
      "if any.") {
      |s| $backup_suffix = s
    }

    opts.on("-D", "--debug",
      "Turn on debug mode.") {
      |b| $debug = b and $verbose = true
    }

    opts.on("-e", "--execute=COMMANDLINE",
      "Run COMMANDLINE for each file in which the following" << nextline <<
      "placeholders can be used:" << nextline <<
      "  %0: replaced by the original file path" << nextline <<
      "  %1: replaced by the source file path" << nextline <<
      "  %2: replaced by the destination file path" << nextline <<
      "  %%: replaced by a %" << nextline <<
      "Missing %2 indicates %1 is modified destructively," << nextline <<
      "and missing both %1 and %2 implies \"(...) < %1 > %2\"" << nextline <<
      "around the command line.") {
      |s| filters << FileFilter.new($config.expand_alias(s))
    }

    opts.on("-f", "--force",
      "Force editing even if a file is read-only.") {
      |b| $force = b
    }

    opts.on("-i", "--preserve-inode",
      "Make sure to preserve the inode number of each file.") {
      |b| $preserve_inode = b
    }

    opts.on("-n", "--dry-run",
      "Just show what would have been done.") {
      |b| $dry_run = b and $verbose = true
    }

    opts.on("-s", "--same-directory",
      "Create a temporary file in the same directory as" << nextline <<
      "each replaced file.") {
      |b| $same_directory = b
    }

    opts.on("-t", "--preserve-timestamp",
      "Preserve the modification time of each file.") {
      |b| $preserve_time = b
    }

    opts.on("-v", "--verbose",
      "Turn on verbose mode.") {
      |b| $verbose = b
    }

    opts.on("-z", "--accept-empty",
      "Accept empty (zero-sized) output.") {
      |b| $accept_empty = b
    }
  }

  setup()

  files = opts.order(*argv)

  if filters.empty? && !files.empty?
    filters << FileFilter.new($config.expand_alias(files.shift))
  end

  if files.empty?
    STDERR.puts "No files to process given.", ""
    print opts
    exit 2
  end

  case filters.size
  when 0
    STDERR.puts "No filter command line to execute given.", ""
    print opts
    exit 1
  when 1
    filter = filters.first

    files.each { |file|
      begin
        filter.filter!(file, file)
      rescue => e
        STDERR.puts "#{file}: skipped: #{e}"
      end
    }
  else
    files.each { |file|
      tmpfile = FileFilter.make_tmpfile_for(file)

      first, last = 0, filters.size - 1

      begin
        filters.each_with_index { |filter, i|
          if i == first
            filter.filter(file, file, tmpfile)
          elsif i == last
            filter.filter(file, tmpfile, file)
          else
            filter.filter!(file, tmpfile)
          end
        }
      rescue => e
        STDERR.puts "#{file}: skipped: #{e}"
      end
    }
  end
rescue OptionParser::ParseError => e
  STDERR.puts "#{MYNAME}: #{e}", usage
  exit 64
rescue => e
  STDERR.puts "#{MYNAME}: #{e}"
  exit 1
end

#setupObject



211
212
213
214
215
216
# File 'lib/inplace.rb', line 211

def setup
  $backup_suffix = nil
  $debug = $verbose =
    $dereference = $force = $dry_run = $same_directory =
    $preserve_inode = $preserve_time = $accept_empty = false
end

#uninterruptibleObject



542
543
544
545
546
547
548
549
550
551
# File 'lib/inplace.rb', line 542

def uninterruptible
  orig = $uninterruptible
  $uninterruptible = true

  yield

  interrupt if $interrupt
ensure
  $uninterruptible = orig
end