Canonical OC import import

Time:2022-5-19

preface

When developing OC projects, we often use third-party libraries. For example, we will import afnetworking for network requests, or use masonry for layout constraints. With the expansion of the project and the increase of developers, the project specification and format will become worse and worse. In order to standardize the code style and format, there will be special discussions on code specification within the general group. However, it is inevitable that there are some omissions. For example, the third-party library is not reasonably imported in the way recommended by WWDC (#import < A / A.H >).

As we know, it is generally used when introducing other third-party libraries#Library type < import / import h>Enter the import mode. As for why we import in this way, one is to clarify the origin of the header file and avoid problems, and the other is whether we open it or notClang ModuleWhen, switch at will.

How to solve the above import problem? Next, I will introduce a small tool developed in Python to solve the above problems.

Project introduction

This projectIs a small tool developed using python, which only needs to be runmain.pyAfter the file is, select the project you want to modify to replace the project globally h\. M file third-party library import problem.

Canonical OC import import

Operation pop-up

After running, you can see the following results:

#Import "masonry. H" // import method of unprocessed
#import <Masonry/Masonry. h> // import method after processing

Implementation principle:

1. Traverse the files under the pods path to generate a {‘third party library / A.H’, ‘third party library / B.H’…} This is an array in the format. Let me pick some key codes to introduce

for dir_name in files:
    if os.path.isdir(dir_path):
        dot_h_array = []
        for _, _, files in os.walk(dir_path):
            for file in files:
                if file.endswith('.h'):
                    dot_h_array.append(file)

        pods_dot_h[dir_name] = dot_h_array

#Rearrange pods_ dot_ H dictionary, convert the dictionary into a dictionary in the format of {'XX. H' = > 'pod name / XX. H'}
for key in pods_dot_h:
    for value in pods_dot_h[key]:
        pods_import[value] = f"{key}/{value}"

There are about two steps here,

  • Traverse a folder under pods, such asAFNetworkingPath, and then create a similar one: afnetworking = {‘afsecuritypolicy. H’, ‘afurlsessionmanager. H’…} Style dictionary
  • staypods_import[value] = f"{key}/{value}"This lineAFNetworkingThe class in implements a format conversion similar to {afsecuritypolicy. H = “afnetworking / afsecuritypolicy. H”…} Such an array

The main significance of this is to facilitate the traversal h\. Encountered in m fileAFSecurityPolicy.hClass, we can easily replace it withAFNetworking/AFSecurityPolicy.h

2. Traverse the in the project h\. m. Use regular matching#Import "class name. H"Such an import method, and obtain the current importedClass name h, if the current class name is in the collection returned above, the current import will be changed to import angle brackets

import_dot_hfile = re.compile(r'#import[\s]*["|<][\w\.\+]+\.h["|>]')
dot_hfile = re.compile(r'[\w]+[\w\.\+]+\.h')

for line in finput:
    match = import_dot_hfile.search(line)
    if match:
        old_str = match.group()
        import_name = dot_hfile.search(old_str).group()
        if import_name in pods_import:
            new_str = f"#import <{pods_import[import_name]}>"
            line = line.replace(old_str, new_str)
    sys.stdout.write(line)

There are three steps:

  • Use regularimport_dot_hfileTraverse each line of a file if it matchesImport "class name. H", we use regular to get the imported data of this rowClass name h
  • According to the usedClass name h, frompods_importFind the complete spelling containing the third-party library name and package it into#Import < library name / class name h>New line string in format
  • Replace the current line with a new string and write to the file

project

The project address is:https://github.com/xiaofeixia9/ImportTool

Summary

In fact, Python is far more than this chapter, except beforePython environment configurationIn addition to the functions introduced, I also encountered the problem of transferring interface documents to IOS development h\. M model file, parsing excel and other requirements, which greatly simplifies the time required for manual retrieval and improves the development efficiency.

Recommended Today

Value transfer between Vue components

There are three main types of value transfer between Vue components: Parent componentPass value toSubcomponents SubcomponentsPass value toParent component assemblyValue transfer between 1. The parent component passes the value to the child component adoptpropsValue transmission The parent component passes the value to the child component, which is mainly customized by the componentpropsProperty, which is used […]