All files / src/repo/utils zcl_abapgit_zip.clas.abap

100% Statements 363/363
100% Branches 0/0
100% Functions 0/0
100% Lines 363/363

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 3641x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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) TYPE xstring
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS export
      IMPORTING
        !is_local_settings TYPE zif_abapgit_persistence=>ty_repo-local_settings
        !iv_package        TYPE devclass
        !io_dot_abapgit    TYPE REF TO zcl_abapgit_dot_abapgit
        !iv_show_log       TYPE abap_bool DEFAULT abap_true
        !it_filter         TYPE zif_abapgit_definitions=>ty_tadir_tt OPTIONAL
      RETURNING
        VALUE(rv_xstr)     TYPE xstring
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS export_object
      IMPORTING
        !iv_object_type        TYPE trobjtype
        !iv_object_name        TYPE sobj_name
        !iv_main_language_only TYPE abap_bool DEFAULT abap_false
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS export_package
      IMPORTING
        !iv_package        TYPE devclass
        !iv_folder_logic   TYPE string
        !iv_main_lang_only TYPE abap_bool
        !iv_ign_subpkg     TYPE abap_bool OPTIONAL
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS load
      IMPORTING
        !iv_xstr        TYPE xstring
      RETURNING
        VALUE(rt_files) TYPE zif_abapgit_git_definitions=>ty_files_tt
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS save_binstring_to_localfile
      IMPORTING
        !iv_filename  TYPE string
        !iv_binstring TYPE xstring
      RAISING
        zcx_abapgit_exception .
  PROTECTED SECTION.
  PRIVATE SECTION.
 
    CLASS-METHODS filename
      IMPORTING
        !iv_str      TYPE string
      EXPORTING
        !ev_path     TYPE string
        !ev_filename TYPE string
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS normalize_path
      CHANGING
        !ct_files TYPE zif_abapgit_git_definitions=>ty_files_tt
      RAISING
        zcx_abapgit_exception .
    CLASS-METHODS unzip_file
      IMPORTING
        !iv_xstr        TYPE xstring
      RETURNING
        VALUE(rt_files) TYPE zif_abapgit_git_definitions=>ty_files_tt
      RAISING
        zcx_abapgit_exception .
ENDCLASS.
 
 
 
CLASS zcl_abapgit_zip IMPLEMENTATION.
 
 
  METHOD encode_files.
 
    DATA: lo_zip      TYPE REF TO cl_abap_zip,
          lv_filename TYPE string.
 
    FIELD-SYMBOLS: <ls_file> LIKE LINE OF it_files.
 
 
    CREATE OBJECT lo_zip.
 
    LOOP AT it_files ASSIGNING <ls_file>.
      CONCATENATE <ls_file>-file-path+1 <ls_file>-file-filename INTO lv_filename.
      lo_zip->add( name    = lv_filename
                   content = <ls_file>-file-data ).
    ENDLOOP.
 
    rv_xstr = lo_zip->save( ).
 
  ENDMETHOD.
 
 
  METHOD export.
 
    DATA li_log       TYPE REF TO zif_abapgit_log.
    DATA lt_zip       TYPE zif_abapgit_definitions=>ty_files_item_tt.
    DATA lo_serialize TYPE REF TO zcl_abapgit_serialize.
 
    CREATE OBJECT li_log TYPE zcl_abapgit_log.
    li_log->set_title( 'Zip Export Log' ).
 
    IF zcl_abapgit_factory=>get_sap_package( iv_package )->exists( ) = abap_false.
      zcx_abapgit_exception=>raise( |Package { iv_package } doesn't exist| ).
    ENDIF.
 
    CREATE OBJECT lo_serialize
      EXPORTING
        io_dot_abapgit    = io_dot_abapgit
        is_local_settings = is_local_settings.
 
    lt_zip = lo_serialize->files_local(
      iv_package = iv_package
      ii_log     = li_log
      it_filter  = it_filter ).
 
    FREE lo_serialize.
 
    IF li_log->count( ) > 0 AND iv_show_log = abap_true.
      zcl_abapgit_log_viewer=>show_log( li_log ).
    ENDIF.
 
    rv_xstr = encode_files( lt_zip ).
 
  ENDMETHOD.
 
 
  METHOD export_object.
 
    DATA: ls_tadir         TYPE zif_abapgit_definitions=>ty_tadir,
          lv_folder        TYPE string,
          lv_fullpath      TYPE string,
          lv_sep           TYPE c LENGTH 1,
          ls_files_item    TYPE zif_abapgit_objects=>ty_serialization,
          lo_frontend_serv TYPE REF TO zif_abapgit_frontend_services.
 
    FIELD-SYMBOLS: <ls_file> LIKE LINE OF ls_files_item-files.
 
    ls_tadir = zcl_abapgit_factory=>get_tadir( )->read_single(
        iv_object   = iv_object_type
        iv_obj_name = iv_object_name ).
 
    IF ls_tadir IS INITIAL.
      zcx_abapgit_exception=>raise( 'Object could not be found' ).
    ENDIF.
 
    ls_files_item-item-obj_type = ls_tadir-object.
    ls_files_item-item-obj_name = ls_tadir-obj_name.
    ls_files_item-item-abap_language_version = '*'. "any
 
    ls_files_item = zcl_abapgit_objects=>serialize(
      is_item        = ls_files_item-item
      io_i18n_params = zcl_abapgit_i18n_params=>new(
        iv_main_language_only = iv_main_language_only
        iv_main_language      = sy-langu ) ).
 
    IF lines( ls_files_item-files ) = 0.
      zcx_abapgit_exception=>raise( 'Empty' ).
    ENDIF.
 
    lo_frontend_serv = zcl_abapgit_ui_factory=>get_frontend_services( ).
    lo_frontend_serv->directory_browse( CHANGING cv_selected_folder = lv_folder ).
    IF lv_folder IS INITIAL.
      RAISE EXCEPTION TYPE zcx_abapgit_cancel.
    ENDIF.
 
    lo_frontend_serv->get_file_separator( CHANGING cv_file_separator = lv_sep ).
 
    LOOP AT ls_files_item-files ASSIGNING <ls_file>.
      lv_fullpath = |{ lv_folder }{ lv_sep }{ <ls_file>-filename }|.
      save_binstring_to_localfile( iv_filename  = lv_fullpath
                                   iv_binstring = <ls_file>-data ).
 
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD export_package.
 
    DATA: ls_local_settings  TYPE zif_abapgit_persistence=>ty_repo-local_settings,
          lo_dot_abapgit     TYPE REF TO zcl_abapgit_dot_abapgit,
          lo_frontend_serv   TYPE REF TO zif_abapgit_frontend_services,
          lv_default         TYPE string,
          lv_package_escaped TYPE string,
          lv_path            TYPE string,
          lv_zip_xstring     TYPE xstring.
 
    ls_local_settings-main_language_only = iv_main_lang_only.
    ls_local_settings-ignore_subpackages = iv_ign_subpkg.
 
    lo_dot_abapgit = zcl_abapgit_dot_abapgit=>build_default( ).
    lo_dot_abapgit->set_folder_logic( iv_folder_logic ).
 
    lo_frontend_serv = zcl_abapgit_ui_factory=>get_frontend_services( ).
 
    lv_package_escaped = iv_package.
    REPLACE ALL OCCURRENCES OF '/' IN lv_package_escaped WITH '#'.
    lv_default = |{ lv_package_escaped }_{ sy-datlo }_{ sy-timlo }.zip|.
 
    lv_zip_xstring = export(
      is_local_settings = ls_local_settings
      iv_package        = iv_package
      io_dot_abapgit    = lo_dot_abapgit ).
 
    lv_path = lo_frontend_serv->show_file_save_dialog(
      iv_title            = 'Package Export'
      iv_extension        = 'zip'
      iv_default_filename = lv_default ).
 
    lo_frontend_serv->file_download(
      iv_path = lv_path
      iv_xstr = lv_zip_xstring ).
 
  ENDMETHOD.
 
 
  METHOD filename.
 
    IF iv_str CA '/'.
      FIND REGEX '(.*/)(.*)' IN iv_str
        SUBMATCHES ev_path ev_filename.
      IF sy-subrc <> 0.
        zcx_abapgit_exception=>raise( 'Malformed path' ).
      ENDIF.
      IF ev_path <> '/'.
        CONCATENATE '/' ev_path INTO ev_path.
      ENDIF.
    ELSE.
      ev_path = '/'.
      ev_filename = iv_str.
    ENDIF.
    TRANSLATE ev_filename TO LOWER CASE.
 
  ENDMETHOD.
 
 
  METHOD load.
 
    rt_files = unzip_file( iv_xstr ).
 
  ENDMETHOD.
 
 
  METHOD normalize_path.
* removes first folder from path if needed
 
    DATA: lt_split  TYPE TABLE OF string,
          lv_needed TYPE abap_bool,
          lv_length TYPE i,
          lv_split  LIKE LINE OF lt_split.
 
    FIELD-SYMBOLS: <ls_file> LIKE LINE OF ct_files.
 
 
    READ TABLE ct_files INDEX 1 ASSIGNING <ls_file>.
    IF sy-subrc <> 0.
      RETURN.
    ENDIF.
 
    SPLIT <ls_file>-path AT '/' INTO TABLE lt_split.
    IF sy-subrc <> 0.
      RETURN.
    ENDIF.
    READ TABLE lt_split INDEX 2 INTO lv_split.
    IF sy-subrc <> 0 OR strlen( lv_split ) = 0.
      RETURN.
    ENDIF.
 
    CONCATENATE '/' lv_split '/*' INTO lv_split.
 
    lv_needed = abap_true.
    LOOP AT ct_files ASSIGNING <ls_file>.
      IF NOT <ls_file>-path CP lv_split.
        lv_needed = abap_false.
        EXIT. " current loop
      ENDIF.
    ENDLOOP.
 
    IF lv_needed = abap_true.
      lv_length = strlen( lv_split ) - 2.
      LOOP AT ct_files ASSIGNING <ls_file>.
        <ls_file>-path = <ls_file>-path+lv_length.
      ENDLOOP.
    ENDIF.
 
  ENDMETHOD.
 
 
  METHOD save_binstring_to_localfile.
 
    zcl_abapgit_ui_factory=>get_frontend_services( )->file_download(
      iv_path = iv_filename
      iv_xstr = iv_binstring ).
 
  ENDMETHOD.
 
 
  METHOD unzip_file.
 
    DATA: lo_zip  TYPE REF TO cl_abap_zip,
          lv_data TYPE xstring.
 
    FIELD-SYMBOLS: <ls_zipfile> LIKE LINE OF lo_zip->files,
                   <ls_file>    LIKE LINE OF rt_files.
 
 
    CREATE OBJECT lo_zip.
    lo_zip->load( EXPORTING
                    zip             = iv_xstr
                  EXCEPTIONS
                    zip_parse_error = 1
                    OTHERS          = 2 ).
    IF sy-subrc <> 0.
      zcx_abapgit_exception=>raise( 'error from zip' ).
    ENDIF.
 
    LOOP AT lo_zip->files ASSIGNING <ls_zipfile>.
 
      lo_zip->get(
        EXPORTING
          name                    = <ls_zipfile>-name
        IMPORTING
          content                 = lv_data
        EXCEPTIONS
          zip_index_error         = 1
          zip_decompression_error = 2
          OTHERS                  = 3 ).
      IF sy-subrc <> 0.
        zcx_abapgit_exception=>raise( 'error from zip get' ).
      ENDIF.
 
      APPEND INITIAL LINE TO rt_files ASSIGNING <ls_file>.
 
      filename(
        EXPORTING
          iv_str      = <ls_zipfile>-name
        IMPORTING
          ev_path     = <ls_file>-path
          ev_filename = <ls_file>-filename ).
 
      <ls_file>-data = lv_data.
 
      <ls_file>-sha1 = zcl_abapgit_hash=>sha1_blob( <ls_file>-data ).
 
    ENDLOOP.
 
    DELETE rt_files WHERE filename IS INITIAL.
 
    normalize_path( CHANGING ct_files = rt_files ).
 
  ENDMETHOD.
ENDCLASS.