module MxxRu::Util

Constants

CALLER_ENTRY_REGEX

Regular expression to parse entries in Kernel#caller result.

IS_WINDOWS_PLATFORM

This flag is set to true on Windows.

Since v.1.5.1

MATCH_FILE_EXT_REGEX

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

MXXARG_BRIEF_DESC
MXXARG_BRIEF_DESC_DISABLED
MXXARG_CLEAN

Ruby command line parameters, handled by MxxRu.

MXXARG_DRY_RUN
MXXARG_KEEP_TMPS
MXXARG_REBUILD
MXXARG_SHOW_CMD
MXXARG_SHOW_TMPS
Tmp_files

Class, responsible for creation and removal of temporary files. Temporary files are created in current folder. Temporary files are removed before application is exitted, if –mxx-keep-tmps option isn't set.

Public Class Methods

build_call_wrapper( a_target ) click to toggle source

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.

# 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 ) click to toggle source

Returns filename with new last extension.

# 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 ) click to toggle source

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.

# 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.exist?( d )
      Dir.mkdir( d )
    end
  }
end
delete_file( a_name ) click to toggle source

Deleting file name without exception.

# File lib/mxx_ru/util.rb, line 219
def Util.delete_file( a_name )
  if FileTest.exist?( a_name )
    File.delete( a_name )
  end
end
ensure_path_exists( a_path_name ) click to toggle source

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.

# 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.exist?( a_path_name )
      MxxRu::Util.create_dir( a_path_name )
    end
  end
end
host_os() click to toggle source

Get OS name where script is exeuted.

# 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 ) click to toggle source

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!*

# 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 ) click to toggle source

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

# 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 ) click to toggle source

Returns file name without last extension.

# 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