Module: MxxRu::Util

Defined in:
lib/mxx_ru/util.rb

Defined Under Namespace

Classes: Mode, TmpFiles

Constant Summary collapse

MATCH_FILE_EXT_REGEX =

Regular expression to divide file name and it’s extension.

Regexp.new( "^(.+)\\.([^\\./]+)$" )
MXXARG_CLEAN =

Ruby command line parameters, handled by MxxRu.

'--mxx-clean'
MXXARG_REBUILD =
'--mxx-rebuild'
MXXARG_SHOW_CMD =
'--mxx-show-cmd'
MXXARG_KEEP_TMPS =
'--mxx-keep-tmps'
MXXARG_SHOW_TMPS =
'--mxx-show-tmps'
MXXARG_DRY_RUN =
'--mxx-dry-run'
MXXARG_BRIEF_DESC =
'--mxx-brief-show'
MXXARG_BRIEF_DESC_DISABLED =
'--mxx-brief-hide'
CALLER_ENTRY_REGEX =

Regular expression to parse entries in Kernel#caller result.

/^(.+):\d+(:in\s.+){0,1}$/
Tmp_files =

For compatibility with previous versions.

TmpFiles
IS_WINDOWS_PLATFORM =

This flag is set to true on Windows.

Since v.1.5.1

!((RbConfig::CONFIG['host_os'] =~ /mswin|mingw/).nil?)
@@host_os =

OS name, script is run on.

nil

Class Method Summary collapse

Class Method Details

.build_call_wrapper(a_target) ⇒ Object

Auxiliary function for correct execution of build method of target object. Before execution TmpFiles.push is executed, and after execution of build method TmpFiles.pop is called. This way we are removing temporary files created during a compilation of given target.



306
307
308
309
310
311
312
313
314
# File 'lib/mxx_ru/util.rb', line 306

def Util.build_call_wrapper( a_target )
  begin
    TmpFiles.instance.push
    state = a_target.build
    return state
  ensure
    TmpFiles.instance.pop
  end
end

.change_file_ext(name, ext) ⇒ Object

Returns filename with new last extension.



214
215
216
# File 'lib/mxx_ru/util.rb', line 214

def Util.change_file_ext( name, ext )
  remove_file_ext( name ) + ext
end

.create_dir(a_name) ⇒ Object

Create folder if it isn’t exist.

All chain of names is tracked. For example, if we trying to create output/release/lib folder, and only output is exists, then release/lib folders are created.



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/mxx_ru/util.rb', line 321

def Util.create_dir( a_name )
  # Dividing the chain.
  chain = Array.new
  chain.unshift( a_name )

  to_split = a_name.clone
  while to_split != '.' 
    r = File.split( to_split )

    # We must stop if to_split cannot be splitted anymore
    # (when working with absolute paths in to_split can be drive name
    # on Windows platform (e.g. 'D:\') or root directory on Unix).
    # In this case r will be equal to [ <something>, '/' ].
    break if 2 == r.size and ( '\\' == r[ 1 ] or '/' == r[ 1 ] )

    chain.unshift( to_split = r[ 0 ] )
  end

  chain.each { |d|
    if !FileTest.exists?( d )
      Dir.mkdir( d )
    end
  }
end

.delete_file(a_name) ⇒ Object

Deleting file name without exception.



219
220
221
222
223
# File 'lib/mxx_ru/util.rb', line 219

def Util.delete_file( a_name )
  if FileTest.exists?( a_name )
    File.delete( a_name )
  end
end

.ensure_path_exists(a_path_name) ⇒ Object

To ensure folder is exist.

If dry-run mode wasn’t set, checking for existance. If it isn’t exists, trying to create it.

In a dry-run mode MxxRu always assumes folder is exists, even if it isn’t.



353
354
355
356
357
358
359
# File 'lib/mxx_ru/util.rb', line 353

def Util.ensure_path_exists( a_path_name )
  if !MxxRu::Util::Mode.instance.is_dry_run
    if !FileTest.exists?( a_path_name )
      MxxRu::Util.create_dir( a_path_name )
    end
  end
end

.host_osObject

Get OS name where script is exeuted.



362
363
364
365
366
367
368
# File 'lib/mxx_ru/util.rb', line 362

def Util.host_os
  if !@@host_os
    @@host_os = RbConfig::CONFIG[ "host_os" ]
  end

  return @@host_os
end

.native_pathname(a_pathname) ⇒ Object

Transform separators in file names to correct ones on the given platform.

For example on mswin32 we change “/” to “".

a_pathname

Path to the file in unix style.

*IMPORTANT! In current version only mswin32 platform is tracked!*



383
384
385
386
387
388
389
# File 'lib/mxx_ru/util.rb', line 383

def Util.native_pathname( a_pathname )
  if IS_WINDOWS_PLATFORM
    return a_pathname.gsub( "/", "\\" )
  end

  return a_pathname
end

.prj_alias_form_caller(caller_result) ⇒ Object

Extract file name from ‘Kernel#caller’ result for using this name as project alias.



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/mxx_ru/util.rb', line 393

def Util.prj_alias_form_caller( caller_result )
  r = CALLER_ENTRY_REGEX.match( caller_result[ 0 ] )[ 1 ]
  # Name can starts with './' ('./some/module/prj.rb'). That name
  # must be transformed to 'some/module/prj.rb'.
  r = if r[ 0..1 ] == './'
      r[ 2...r.size ]
    else
      r
    end
  # Path could be absolute. We should extract current path name from it.
  p = Pathname.new( r )
  if p.absolute?
    p = p.relative_path_from( Pathname.pwd )
  end

  p.to_s
end

.remove_file_ext(a_name) ⇒ Object

Returns file name without last extension.



203
204
205
206
207
208
209
210
211
# File 'lib/mxx_ru/util.rb', line 203

def Util.remove_file_ext( a_name )
  match_result = MATCH_FILE_EXT_REGEX.match( a_name )
  if nil == match_result
    # No extension was present.
    return String.new( a_name )
  end

  return match_result[ 1 ]
end