All files / src/background zcl_abapgit_background.clas.abap

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

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 2071x 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 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 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 1x 1x 1x 1x 1x 1x 1x  
CLASS zcl_abapgit_background DEFINITION
  PUBLIC
  CREATE PUBLIC .
 
  PUBLIC SECTION.
 
    TYPES: BEGIN OF ty_method,
             class       TYPE seoclsname,
             description TYPE string,
           END OF ty_method.
 
    TYPES: ty_methods TYPE SORTED TABLE OF ty_method WITH UNIQUE KEY class.
 
    CLASS-METHODS run
      RAISING
        zcx_abapgit_exception .
 
    CLASS-METHODS list_methods
      RETURNING VALUE(rt_methods) TYPE ty_methods.
 
    CLASS-METHODS enqueue
      RAISING
        zcx_abapgit_exception.
 
    CLASS-METHODS dequeue.
  PROTECTED SECTION.
  PRIVATE SECTION.
    CONSTANTS c_enq_type TYPE c LENGTH 12 VALUE 'BACKGROUND'.
    CONSTANTS c_interface TYPE seoclskey VALUE 'ZIF_ABAPGIT_BACKGROUND'.
ENDCLASS.
 
 
 
CLASS ZCL_ABAPGIT_BACKGROUND IMPLEMENTATION.
 
 
  METHOD dequeue.
    CALL FUNCTION 'DEQUEUE_EZABAPGIT'
      EXPORTING
        type = c_enq_type.
  ENDMETHOD.
 
 
  METHOD enqueue.
    CALL FUNCTION 'ENQUEUE_EZABAPGIT'
      EXPORTING
        mode_zabapgit  = 'E'
        type           = c_enq_type
        _scope         = '3'
      EXCEPTIONS
        foreign_lock   = 1
        system_failure = 2
        OTHERS         = 3.
 
    IF sy-subrc <> 0.
      zcx_abapgit_exception=>raise_t100( ).
    ENDIF.
  ENDMETHOD.
 
 
  METHOD list_methods.
 
    DATA: ls_method          LIKE LINE OF rt_methods,
          lt_implementing    TYPE seor_implementing_keys,
          ls_implementing    LIKE LINE OF lt_implementing,
          lt_interf          TYPE abap_intfdescr_tab,
          lt_local_classes   TYPE STANDARD TABLE OF scompo,
          lv_classname       TYPE string,
          lr_typedescr       TYPE REF TO cl_abap_typedescr,
          lr_typedescr_class TYPE REF TO cl_abap_classdescr.
 
    FIELD-SYMBOLS: <ls_local_class> LIKE LINE OF lt_local_classes,
                   <ls_method>      LIKE LINE OF rt_methods.
 
    IF zcl_abapgit_factory=>get_environment( )->is_merged( ) = abap_true.
      " Assume the standalone version runs.
      CALL FUNCTION 'WB_TREE_GET_OBJECTS'
        EXPORTING
          include = ' '
          otype   = 'L'
          program = sy-repid
        TABLES
          olist   = lt_local_classes.
 
      LOOP AT lt_local_classes ASSIGNING <ls_local_class>.
        lv_classname = |\\PROGRAM={ sy-repid }\\CLASS={ <ls_local_class>-name }|.
        cl_abap_typedescr=>describe_by_name(
         EXPORTING
           p_name         = lv_classname
         RECEIVING
           p_descr_ref    = lr_typedescr
         EXCEPTIONS
           type_not_found = 1
           OTHERS         = 2 ).
 
        IF sy-subrc = 0 AND lr_typedescr IS BOUND.
          lr_typedescr_class ?= lr_typedescr.
          IF lr_typedescr_class IS BOUND.
            lt_interf = lr_typedescr_class->interfaces.
            READ TABLE lt_interf WITH TABLE KEY name = c_interface TRANSPORTING NO FIELDS.
            IF sy-subrc = 0.
              ls_method-class = <ls_local_class>-name.
              INSERT ls_method INTO TABLE rt_methods.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDLOOP.
 
    ELSE.
      " Assume the developer version runs.
      CALL FUNCTION 'SEO_INTERFACE_IMPLEM_GET_ALL'
        EXPORTING
          intkey       = c_interface
        IMPORTING
          impkeys      = lt_implementing
        EXCEPTIONS
          not_existing = 1
          OTHERS       = 2.
      IF sy-subrc = 0.
        LOOP AT lt_implementing INTO ls_implementing.
          ls_method-class = ls_implementing-clsname.
          INSERT ls_method INTO TABLE rt_methods.
        ENDLOOP.
      ENDIF.
    ENDIF.
 
    LOOP AT rt_methods ASSIGNING <ls_method>.
      CALL METHOD (<ls_method>-class)=>zif_abapgit_background~get_description
        RECEIVING
          rv_description = <ls_method>-description.
    ENDLOOP.
 
  ENDMETHOD.
 
 
  METHOD run.
 
    DATA: lo_per        TYPE REF TO zcl_abapgit_persist_background,
          lo_repo       TYPE REF TO zcl_abapgit_repo_online,
          lt_list       TYPE zcl_abapgit_persist_background=>ty_background_keys,
          li_background TYPE REF TO zif_abapgit_background,
          li_log        TYPE REF TO zif_abapgit_log,
          lx_error      TYPE REF TO zcx_abapgit_exception,
          lv_repo_name  TYPE string.
 
    FIELD-SYMBOLS: <ls_list> LIKE LINE OF lt_list.
 
    TRY.
        enqueue( ).
      CATCH zcx_abapgit_exception.
        WRITE: / 'Another instance of the program is already running'.
        RETURN.
    ENDTRY.
 
    CREATE OBJECT lo_per.
    lt_list = lo_per->list( ).
 
    WRITE: / 'Background mode'.
 
    LOOP AT lt_list ASSIGNING <ls_list>.
      CREATE OBJECT li_log TYPE zcl_abapgit_log.
 
      TRY.
          lo_repo ?= zcl_abapgit_repo_srv=>get_instance( )->get( <ls_list>-key ).
          lv_repo_name = lo_repo->get_name( ).
          WRITE: / <ls_list>-method, lv_repo_name.
 
          zcl_abapgit_login_manager=>set(
            iv_uri      = lo_repo->get_url( )
            iv_username = <ls_list>-username
            iv_password = <ls_list>-password ).
 
          TRY.
              CREATE OBJECT li_background TYPE (<ls_list>-method).
 
              li_background->run(
                io_repo     = lo_repo
                ii_log      = li_log
                it_settings = <ls_list>-settings ).
            CATCH cx_sy_create_object_error.
              li_log->add_warning( |{ <ls_list>-method } could not be executed,|
                                 & | as it is not accessible (local/global class).| ).
          ENDTRY.
 
          " Decrease memory usage for repository already processed (but keep log)
          lo_repo->refresh(
            iv_drop_cache = abap_true
            iv_drop_log   = abap_false ).
        CATCH zcx_abapgit_exception INTO lx_error.
          li_log->add_exception( lx_error ).
      ENDTRY.
 
      " Clear auth buffer to allow different user/password per repository in background mode
      zcl_abapgit_login_manager=>clear( ).
 
      zcl_abapgit_log_viewer=>write_log( li_log ).
    ENDLOOP.
 
    IF lines( lt_list ) = 0.
      WRITE: / 'Nothing configured'.
    ENDIF.
 
    dequeue( ).
 
  ENDMETHOD.
ENDCLASS.