Giter Club home page Giter Club logo

ocannotation's Introduction

Introduction

OCAnnotation is a light-weighted framework empowering the Objective-C language with the ability of annotation. As in Java, annotation is a form of syntactic metadata that can be added to the source code. It provides a handy way to apply certain behaviors to related program elements, such as class, methods, etc. By embedding this feature, we could make Objective-C a more flexible language, and bring our iOS development work more convenience and possibilities.

This framework is developed and maintained by the Ant Wealth software development team. It is already applied in our project in multiple modules, including but not limited to logging,network layer and data layer. We would like to share this exquisite tool with you, and be expectantly looking forward to you exploring its usage with all your imagination.

Feature

  • support annotations in all .h/.m files
  • support annotations added to @interface, @implementation, @property, method, etc.
  • support custom definition of annotation type
  • support custom definition of annotation key-values

Installation

  1. Download the .gem and install it:
gem install ocannotation
  1. Copy the config file (.oca.config) to your project's root path, modify your custom configuration as needed.
  2. Under your project's directory, run the set-up command (we also provide command oca_clean, you could use it to reset your status):
oca_setup

If you run it successfully, you would get the result much like the following:

  1. Add OCAnnotation in your Podfile:
pod 'OCAnnotation'

Well now you are all set to go~

How to Use

Setup

Basically the mistery behind this tool is to scan all annotations in your project and do the modeling. Thus, at the launch time of your app, some setup work is required to be done. Here is an example code segment:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    OCAAnnotationManager *annotationManager = [OCAAnnotationManager sharedManager];

    // custom annotation type registration (optional)
    [annotationManager registerAnnotationType:@"RemoteLog"
                                     position:OCAAnnotationPositionMethod
                                        class:[AFWRemoteLogMethodAnnotation class]];

    // annotation setup
    NSDictionary *configs = kAFWAnnotationConfigs; // using the macro name you write in the Annotation/.config file
    [[OCAAnnotationManager sharedManager] addConfigsWithConfigDic:configs];
}

Notice: After the scanning, the generated file (CustomFileName.gen.h) will be copied to your project path. You need to use 'Add file to...' to add file reference to your project so that you could access the generated macro successfully.

Custom Annotations

'Type' is a custom variable representing a certain type of group of annotations. Generally, a type is related to a specific use scenario. To make your own custom annotation type, you are required to create an annotation class for that type, and the class should be a subclass of OCABaseAnnotation. In OCAnnotation.framework,OCABaseAnnotation is the base annotation model class defining some basic behaviors. We also provide OCABaseClassAnnotation/OCABaseMethodAnnotation/OCABasePropertyAnnotation, which are base models for annotations attached to class/method/property. You could inherit them as well. As stated in the section above, you need to register your custom type at launch time:

// custom annotation type registration (optional)
[annotationManager registerAnnotationType:@"RemoteLog"
                                 position:OCAAnnotationPositionMethod
                                    class:[AFWRemoteLogMethodAnnotation class]];

In your subclass, you could do anything magic in the init method, with all the information in annotations packed in the variable 'params' for you. Quick example:

- (instancetype)initWithSourceFile:(NSString *)sourceFile
                         className:(NSString *)className
                            params:(NSDictionary *)params
                    methodSelector:(NSString *)methodSelector
                              type:(NSString *)type
{
    self = [super initWithSourceFile:sourceFile
                           className:className
                              params:params
                      methodSelector:methodSelector
                                type:type];
    if (self) {
        self.remoteLogId = self.parameters[@"remoteLogId"];
        self.remoteLogMethod = self.parameters[@"remoteLogMethod"];
        
        @weakify(self);
        self.onAnnotationCreated = ^(OCABaseAnnotation *anno){
            @strongify(self);
            [self registerRemoteLogAOP];
        };
        
    }
    return self;
}

Add Annotations

Above any program element, add your annotation in format “#pragma annotation(key:"value")”. For example, if you would like to add annotation on @interface UIApplicationDelegate (Different key-value pair can use comma/space as separator. Key and value can be related by colon or equals sign):

#pragma annotation(type:"default",param1:"value1",param2:"value2")
@interface UIApplicationDelegate()

That's it. You have successfully added an annotation to @interface UIApplicationDelegate.

Demo

We provide a demo project named OCAnnotationDemo in which we show the basic usage of this tool. You are welcome to download and play with it.

To-do

  • SwiftAnnotation

ocannotation's People

Contributors

jacklian avatar wrshi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ocannotation's Issues

can not find the config file

can not find the config file In Step2

Copy the config file (.oca.config) to your project's root path, modify your custom configuration as needed.

Could you show me the path it is ? or forget to put it to the resp?

readme看不懂

这个readme是真的看不懂啊,说话说一半。

第一步安装gem install ocannotation 成功了,原来说的.gem在下载的Script里的ocannotation-0.1.1.gem
第二步的是真的搜不到,oca.config没找到呢,显示隐藏文件也没有找到;

运行Demo时遇到几个问题

你好,运行Demo的时候遇到几个问题
1.编译时提示'OCAnnotationAnnotationConfig.gen.h' file not found,好像是文件引用路径配置有点问题,配置的路径是Example/OCAnnotation/OCAnnotationAnnotationConfig.gen.h,而实际生成文件的路径是Example/OCAnnotationAnnotationConfig.gen.h
2.在OCAAppDelegate.m或者OCAViewController.m中添加#pragma annotation...后build,OCAnnotationAnnotationConfig.gen.h中都未生成对应的AnnotationConfigs字典,但是在Tests.m中添加可以成功生成,好像是因为,脚本扫描文件时候,用的是File Group,而Demo中的Group Example for OCAnnotation 与文件目录 OCAnnotation 不匹配导致;
3.修改ruby代码,在Script目录执行rake install的时候,"The validation error was 'ocannotation-0.1.0 contains itself (ocannotation-0.1.0.gem), check your files list'.",是不是把build生成的gem放到pkg/目录就不会有这个问题,不知道说的有没有问题.

再说一下一点想法,简单看了下实现,好像整个实现是基于正则的文件扫描,是不是可以考虑基于语义(预处理,抽象语法树之类的)的实现,依赖字符串,感觉有时候就会遇到上述第2点那样没有考虑到的问题,另外,如果是基于clang-llvm之类的,Swift版本的实现是不是就会快一点。之前看到过一篇文章Implementing a Custom Directive Handler in Clang 不知道是不是也可以来实现项目中的效果。

为啥要用依赖工程?

为啥要在新建出一个Annotaion的依赖工程呢。如果我直接放到我主工程run oca_build应该也可以吧

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.